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?
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.
null can only be assigned to reference type, you cannot assign null to primitive variables e.g. int, double, float, or boolean.
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.
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).
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".
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With