On 31.01.2008 03:18, Lew allegedly wrote:
Brian Goetz is among the notables who have debunked the "set to null
to help GC" myth:
<http://www-128.ibm.com/developerworks/java/library/j-jtp01274.html>
Because allocation and garbage collection at one time imposed
significant performance costs on Java programs, many clever tricks
were developed to reduce these costs, such as object pooling and
nulling. Unfortunately, in many cases these techniques can do more
harm than good to your program's performance.
....
Sun warned of this risk and explained how explicit nulling was
needed in [certain] cases... . Unfortunately, programmers often
take this advice too far, using explicit nulling in the hope of
helping the garbage collector. But in most cases, it doesn't help
the garbage collector at all, and in some cases, it can actually
hurt your program's performance.
Think of it this way - setting a variable to null has nothing to do
with the business logic of the program - it's a hack that is based on
a superstition relating entirely to implementation. Breaking the
logic of your program in order to imagine a performance improvement
is a classic programming mistake.
The following quote from the article you referred to shouldn't be
omitted from this context, IMO:
"There is one case where the use of explicit nulling is not only
helpful, but virtually required, and that is where a reference to an
object is scoped more broadly than it is used or considered valid by the
program's specification."
was needed in [certain] cases", so I felt no need to re-iterate it.