Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which goes on the stack or heap?

I am doing some studying and I came across a question that asks to show the correct memory diagram of the following code:

int [] d1 = new int[5];
d1[0] = 3;

Integer [] d2 = new Integer[5];
d2[0] = new Integer(3);

ArrayList d3 = new ArrayList();
d3.add(3);

Here is my attempt at a memory diagram, but it may be incorrect:

enter image description here

I understand things like objects, instance variables, and "new" instances are all on the heap and things such as local variables and primitive types are on the stack, but I'm still confused when it comes to array types.

Any help is appreciated.

like image 620
blutuu Avatar asked May 16 '13 04:05

blutuu


People also ask

How do you tell if it is in the stack or heap?

Key Difference Between Stack and Heap MemoryStack variables can't be resized whereas Heap variables can be resized. Stack memory is allocated in a contiguous block whereas Heap memory is allocated in any random order. Stack doesn't require to de-allocate variables whereas in Heap de-allocation is needed.

Do arrays go on stack or heap?

Unlike Java, C++ arrays can be allocated on the stack. Java arrays are a special type of object, hence they can only be dynamically allocated via "new" and therefore allocated on the heap.

What gets stored on the stack?

Stack: The role of stack memory includes storage of temporary data when handling function calls (normal stack PUSH and POP operations), storage for local variables, passing of parameters in function calls, saving of registers during exception sequences, etc.

Why should heap be used over a stack?

Stack memory allocation is considered safer as compared to heap memory allocation because the data stored can only be access by owner thread. Memory allocation and de-allocation is faster as compared to Heap-memory allocation. Stack-memory has less storage space as compared to Heap-memory.


1 Answers

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

like image 50
WebServer Avatar answered Nov 12 '22 21:11

WebServer