简体   繁体   中英

does JVM gives memory to local reference variable with value null

I was a puzzled with memory management in java. Everywhere it is written that local variable is given memory in stack. And if that local variable is of object type, then it is given memory in heap. I want to know where does JVM allocates memory for local object reference variable with value null.

public void show(){
Animal animal = null;
}

where does the JVM allocates memory for animal object. Is it in stack? or it does not give any memory because it is marked null. I am sure it is not given memory in heap.

can any one clear away my doubts.

I think the reference animal will be stored in stack itself until the method completes its execution, but the state of the object will be stored in heap.

So in above case the object pointing to null then it'll have null assigned in stack itself, otherwise if you initialize the object then it'll have the address of the object in the heap.

  • In Stack(temporary memory) - For local variables memory is allocated in stack, local variables may include both primitive and reference type.

    for example Animal animal = null; which you have declared and assigned a value inside the method "public void show()" gets memory allocated in stack when the method is called. After the method is executed then the local variable animal is erased from the stack, thus stack is temporary memory.

    animal variable of type Animal(class names are user defined datatypes which are declared using keyword "class") gets memory allocated in stack(local variable) and the variable can hold the References of the object of type Animal(classname) which means ur animal variable of type Animal(classname) present in stack holds references of the Object which is present in heap.

  • In heap- for objects memory is allocated in heap, for instance variables(non-static variables) memory is allocated within the object,for static variables memory is allocated in static constant pool

Just a small addition. That reference will be allocated on the stack and it will weight 4 bytes in general (unless CompressedOops are off, which they are not by default. Flag controlling it is -XX:+UseCompressedOops ). If you disable that property, your references will be 8 bytes long.

There is also additional space when you declare a reference, via the two headers that a JVM will issue for each Object - mark and class words (until we have headerless objects). mark word can not be compressed and it's 8 bytes in size; class word is compressed by default and it weights 4 bytes . There is a flag to control it's compression since java-8:

   -XX:-UseCompressedClassPointers

There will be no additional space for the Object itself (which is stored on the heap - since it's null); otherwise if you would have declared it as new Animal() - it would be the size of the properties of that Animal padded to 8 bytes alignment .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM