Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it help GC to null local variables in Java

I was 'forced' to add myLocalVar = null; statement into finally clause just before leaving method. Reason is to help GC. I was told I will get SMS's during night when server crashes next time, so I better did it :-).

I think this is pointless, as myLocalVar is scoped to method, and will be 'lost' as soon as method exits. Extra nulling just pollutes the code, but is harmless otherwise.

My question is, where does this myth about helping GC come from? (I was referred to "Java memory books") Do you know any article from 'authorities' which explain it in more depth? Is there possibility this is not a myth, but really helps somehow? If so, how? May nulling local variables cause any harm?

To clarify, method look like this:

void method() {   MyClass myLocalVar = null;   try {     myLocalVar = get reference to object;     ... do more here ...   } finally {     if (myLocalVar != null) {       myLocalVar.close(); // it is resource which we should close     }      myLocalVar = null; // THIS IS THE LINE I AM TALKING ABOUT   } } 
like image 914
Peter Štibraný Avatar asked Jan 23 '09 17:01

Peter Štibraný


People also ask

Is null eligible for garbage collection?

Does assigning objects to null in Java impact garbage collection? Not necessarily. An object becomes eligible for garbage collection when there are no live threads anymore that hold a reference to the object. Explicit nulling is simply the practice of setting reference objects to null when you are finished with them.

What is the benefit of setting a variable to null when you no longer need it?

As soon as you use the variable declare it as null or "" because active variables in js can be read using inspect element. Resetting the variable password to null will keep your password information safe.

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.

Is null a garbage value?

Setting an object reference to null only makes it eligible for garbage collection. It does not necessarily free up the memory,which depends on when the garbage collector runs(which depends on JVM).


2 Answers

There was an old piece of Sun documentation, Java Platform Performance (link sadly now broken, and I haven't been able to find a new one), which described a situation where nulling a local variable which dropped out of scope actually had an effect on the GC.

However, the paper referred to a very old version of java. As mentioned in this question (which also contains a précis of the problem described in the paper), this no longer affects current JVM implementations.

like image 142
Bill Michell Avatar answered Oct 02 '22 15:10

Bill Michell


The Java GC is supposed to be "sound" but is not necessarily immediately "complete". In other words, it is designed so that it would never eliminate objects that are still accessible by at least one path (and thus cause a dangling reference). It is not necessarily immediately complete since it might take time until it removes everything that can be removed.

I think that most GC myths come from a misunderstanding of that concept. Many people keep too many instance variables alive, and that causes problems, but that is of course not the issue here.

Other people put the local variables in an instance variable (e.g., by passing it to function), and then think that nullifying the local variable somehow eliminates the variable, which is of course untrue.

Finally, there are people who overrely on the GC and think it would do functional shutdown for them (E.g., close connections when variable is removed) which is of course not the case. I think the source of this line is the "I'm really really done with it but I'm not sure how to ensure that".

So yeah, you're correct that it's unneccessary.

like image 24
Uri Avatar answered Oct 02 '22 16:10

Uri