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
}
}
You are correct. Nulling out a variable that will immediately fall out of scope anyway is unnecessary and makes no difference whatsoever to GC. All it does is clutter the code. In Effective Java 2nd Edition, the author recommends against unnecessary nulling out of local variables. See Effective Java, 2nd Edition, Item 6: Eliminate obsolete object references, for a full writeup.
You can also see this in the article Creating and Destroying Java Objects, at InformIT. Read the entire article to find the place where Joshua Bloch agrees with you.
When a local variable falls out of scope, it is exactly the same as if you null the reference to it.
EDIT: Add link to Effective Java 2nd Edition at Sun website
If you don't need large objects in your local scope anymore, you can give the JVM a hint and set the reference NULL.
But it does not make sense before the return, because this is done by the JVM automatically. So I agree with all postings before.
Not in this case. myLocalVar falls out of scope as soon as the function returns, so setting the reference to null does absolutely nothing.
As you correctly point out, nulling out in this case is totally pointless.
Back on JDK1.3 I did actually have a case with an extremely large object graph that also contained lots of circular references within the graph. Clearing out a few of the references did actually improve GC times quite noticeably.
I am not sure if this would apply with a modern VM. Garbage collectors have become increasingly smarter.
I don't know the technical details, but as far as I can remember, the variable is nothing more than reference from the current stack frame, and until this reference is removed, the object cannot be garbage collected. Now, but explicitly setting it to null, you've made sure that the reference is gone. If you don't you're basically letting the VM decide when this reference is cleared, which might or might not be upon exiting the scope (unlike C++, if the object is located on the stack and MUST be destroyed). It might be when the stack frame is overwritten with the next. I'm not sure if there's actually a VM which does this.
Short answer though, it's unnecessary, the mark and sweep will get it eventually. It's at most a question of time.
There are only two cases where I have found setting a variable to null has been useful: