Is a Java Array of Primitives Stored in Stack or Heap

Where array of primitives stored in JVM memory

Objects are always Heap allocated so your dealCodes will be
allocated there only but the total memory allocated is more than 40 bytes.

12 bytes (Header) + 4 bytes (Length of Array) + 40 bytes (4 bytes * 10 ints) = 56 bytes

Same thing applies to an array of Employee objects as well except that each array element is now a reference to Employee object, so the Shallow Heap occupied by employees array is still 56 bytes while the Retained Heap depends on the size of each Employee object.

You can use VisualVM in JDK_HOME/bin directory to take a snapshot of your program/application, see the memory occupied by each object, both shallow and retained heap sizes.

Java Primitive data type on Stack or Heap?

When a method is called, certain data is placed on the stack. When the method finishes, data is removed from the stack. At other points in a program's execution, data is added to the stack, or removed from it.

Therefore, if you have a variable which is intended to outlive the execution of the method that created it, it needs to be on the heap. This applies both to any objects that you create, and any primitives that are stored within those objects.

However, if a variable is intended to go out of scope shortly after its creation - say, at the end of the method in which it's created, or even earlier, then it's appropriate for that variable to be created on the stack. Local variables and method arguments fit this criterion; if they are primitives, the actual value will be on the stack, and if they are objects, a reference to the object (but not the object itself) will be on the stack.

In your specific example, x is unusable as soon as function1 finishes running. So it's reasonable for it to be created on the stack. At the end of function1, data is effectively removed from the stack, including x. On the other hand, the variable y is expected to still exist for as long as the containing object exists; if it were created on the stack, it would cease to exist once the object constructor which created it finishes running. Therefore y must be created on the heap.

Which goes on the stack or heap?

Any Object on Java lives on heap.

In Java Array is also an Object and hence array Object lives on heap.

Explaination:-

When you write

int a=new int[5],

the (new int[5]) part creates object and hence lives on heap.

Integer x=new Integer(10000)

is also an Object(remember new Operator will always create new Object).

and hence when you wright,

Integer [] d2 = new Integer[5];

it is Array of Integer Object.

As far as ArrayList is considered it is also a class but it wraps array Object and adds dynamic memory to it.
So,

ArrayList d3 = new ArrayList();

again creates Object and hence live on heap.

Consider ArrayList class as:

class ArrayList{
int index=0;
Object[] obj=new Object['some integer value (depends on JVM)'];
public void add(Object o){
obj[index]=o;
index++;
}
//other methods
}

so when you write
d3.add(5) actually d3.add(new Integer(5)) is being called.

Remember one golden rule:
In java whatever Object you create live on HEAP and their reference live on stack.

Proof of array being object:-

int[] a={4,3,1,2};
System.out.println(a instanceof Object);

//prints true

Does initialized java array go onto stack or heap?

You can think of it as always going on the heap.

I believe some smart VMs are able to stack-allocate objects if they can detect it's safe - but conceptually it's on the heap. In particular, all array types are reference types (even if the element type is primitive), so the array variable (which is on the stack) is just a reference to an object, and objects normally go on the heap.

In particular, imagine a small change:

byte[] someMethod() { 
byte[] array = { 0, 0 };
return array;
}

If the array were allocated on the stack, what would the returned reference have to refer to?

Java array using stack space

In a word, no.

The only variables that are stored on the stack are primitives and object references. In your example, the arr reference is stored on the stack, but it references data that is on the heap.

If you're asking this question coming from C++ because you want to be sure your memory is cleaned up, read about garbage collection. In short, Java automatically takes care of cleaning up memory in the heap as well as memory on the stack.



Related Topics



Leave a reply



Submit