Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does setting variable to null clear just the reference? [duplicate]

What happens if I do:

Object obj = new Object();
obj = null;

Does it remove the object from memory, or clear just the reference?

More formally, consider the code:

Object obj = new Object();
Object temp = obj;
obj = null;

Why is temp still not null? Shouldn't it have been removed from memory?

like image 422
Atom 12 Avatar asked Oct 30 '15 17:10

Atom 12


People also ask

What happens when a field or variable is set to null?

What is NULL? You can think of NULL as an unknown or empty value. A variable is NULL until you assign a value or an object to it. This can be important because there are some commands that require a value and generate errors if the value is NULL.

Can you assign null to this reference variable?

null can only be assigned to reference type, you cannot assign null to primitive variables e.g. int, double, float, or boolean.

What happens when object is set to null in Java?

In Java, a null value can be assigned to an object reference of any type to indicate that it points to nothing. The compiler assigns null to any uninitialized static and instance members of reference type.

Can you assign null to this reference variable Java?

We cannot assign null to primitive variables e.g int, double, float, or boolean. If we try to do so, then the compiler will complain. The java instanceof operator which is also known as type comparison operator, tests whether the object is an instance of the specified type (class or subclass or interface).


5 Answers

In your first example:

             +------+
obj -------> |OBJECT|
             +------+

After setting obj to null:

             +------+
obj          |OBJECT|
             +------+

No reference to the object exists so it "doesn't exist" anymore because it has become unreachable.


In your second example:

obj -------> +------+
             |OBJECT|
temp ------> +------+

After setting obj to null:

obj          +------+
             |OBJECT|
temp ------> +------+

You can see that temp still references the object so it still "exists".

like image 180
Yassin Hajaj Avatar answered Oct 18 '22 19:10

Yassin Hajaj


In your first example, obj becomes immediately eligible for garbage collection. It's not yet removed from memory, and attempts to use the reference will result in a NullPointerException.

In your second example, the assignment to temp creates a copy of the reference originally referred to by obj. Setting obj to null wouldn't make the object eligible for garbage collection, so it's very much still around.

like image 29
Makoto Avatar answered Sep 27 '22 19:09

Makoto


Object obj = new Object();
obj = null;

Above: It just removes the reference, does not delete the object. However, GC will clear the object from memory because there is no other reference to that object.

In this:

Object obj = new Object();
Object temp = obj;
obj = null;

temp is still a reference to object, so object stays in memory.

like image 44
Aragorn Avatar answered Oct 18 '22 18:10

Aragorn


Setting an object to null, clears the reference. If there is no longer any reference to that object, as in your first example, the memory can be freed, when the JVM runs the GC (Grarbage Collector) the next time.

As long as there is still a reference to the object (second example) this cannot happen.

Furthermore, there is a difference between freeing the memory by GC and returning the memory back to the OS, so it can be reused. Usually, the JVM keeps once allocated memory until the JVM exists or the OS has run out of free memory.

like image 3
hotzst Avatar answered Oct 18 '22 17:10

hotzst


Java works with garbage collection. So you will NEVER know what time an object will be removed from memory. When you remove the last reference to an object, it is passed to the garbage collector and the garbage collector will delete the object sometimes.

In your second example you create an object and give the reference to obj. Then you reference obj by temp. now you have 2 references to your object. obj AND temp. When you now remove the reference obj, the object is still referenced by temp and so it wont be given to the garbage collector.

like image 2
Thallius Avatar answered Oct 18 '22 18:10

Thallius