Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does assigning objects to null in Java impact garbage collection?

Does assigning an unused object reference to null in Java improve the garbage collection process in any measurable way?

My experience with Java (and C#) has taught me that is often counter intuitive to try and outsmart the virtual machine or JIT compiler, but I've seen co-workers use this method and I am curious if this is a good practice to pick up or one of those voodoo programming superstitions?

like image 479
James McMahon Avatar asked Jan 16 '09 03:01

James McMahon


People also ask

IS NULL object eligible for garbage collection?

By nullifying the reference variable- In this method, programmer assigns null to the reference variables of all those objects which are no longer required. This makes the useless objects automatically eligible for the purpose of garbage collection.

What happens when an object is assigned null in Java?

NullPointerException is thrown in Java when you point to an object with a null value. Java programmers usually encounter this infamous pointer exception when they forget to initialize a variable (because null is the default value for uninitialized reference variables).

Can we assign object as null in Java?

NullPointerException is a runtime exception. In Java, a special null value can be assigned to an object reference.

What triggers Java garbage collection?

When a JVM runs out of space in the storage heap and is unable to allocate any more objects (an allocation failure), a garbage collection is triggered. The Garbage Collector cleans up objects in the storage heap that are no longer being referenced by applications and frees some of the space.


1 Answers

Typically, no.

But like all things: it depends. The GC in Java these days is VERY good and everything should be cleaned up very shortly after it is no longer reachable. This is just after leaving a method for local variables, and when a class instance is no longer referenced for fields.

You only need to explicitly null if you know it would remain referenced otherwise. For example an array which is kept around. You may want to null the individual elements of the array when they are no longer needed.

For example, this code from ArrayList:

public E remove(int index) {     RangeCheck(index);      modCount++;     E oldValue = (E) elementData[index];      int numMoved = size - index - 1;     if (numMoved > 0)          System.arraycopy(elementData, index+1, elementData, index,              numMoved);     elementData[--size] = null; // Let gc do its work      return oldValue; } 

Also, explicitly nulling an object will not cause an object to be collected any sooner than if it just went out of scope naturally as long as no references remain.

Both:

void foo() {    Object o = new Object();    /// do stuff with o } 

and:

void foo() {    Object o = new Object();    /// do stuff with o    o = null; } 

Are functionally equivalent.

like image 199
Mark Renouf Avatar answered Sep 28 '22 14:09

Mark Renouf