Re: variable = null; Memory Cleanup
Angry Moth Town wrote:
On Apr 16, 10:52 am, Eric Sosman <Eric.Sos...@sun.com> wrote:
Angry Moth Town wrote:
I have several developers who insist on using the following method,
claiming that it helps reduce memory use by eliminating resources
faster than the garbage collector. Personally, I don't think this is
effective. Is this a useful method, or should we simply be letting
the garbage collector do its job?
/**
* This method closes the ResultSetMetaData if it is open.
*/
public static void closeResultSetMetaData(ResultSetMetaData rs)
{
try {
rs = null;
} catch (Exception exp) {
exp.printStackTrace();
}
}
This method seems utterly pointless. Are you sure you've
given a proper illustration of the technique you're asking about?
Yes, this is the exact method, copied and pasted, and the explanation
I got was that it "clears up memory faster, because the garbage
collector is too slow."
I'm basically going to tell 25 offshore developers that this is
pointless, but I'm not a Java expert, so I wanted to run it through
the newsgroup before I insisted we change it.
All right, tell them it's pointless. It's pointless
for more than one reason:
- First, setting `rs = null' affects only the method's
own copy of the argument value, and does not affect any copies
the caller might have. If the caller does
ResultSetMetadata myRs = ...;
closeResultSetMetaData(myRs);
then myRs is not changed at all. The object myRs refers to
is still "live" and is not eligible for garbage collection;
the method has made no change whatever in its status.
- Second, the try/catch block is pointless, because there
is nothing in `rs = null' that can throw an Exception. The
writer of the try/catch was a seriously confused person.
- Third, setting all the references to a particular object
to null does not avoid garbage collection, it just makes the
object eligible for collection. If the JVM decides to run the
garbage collector it will do so, and the collector may reclaim
the garbage object's memory. "Clearing up the memory faster"
is almost entirely bogus; all that might happen is that the
memory could be reclaimed sooner. Sooner != faster.
(The "almost" is because some garbage collectors must
work harder to reclaim long-lived objects than those that die
young. Setting all the references to null -- or pointing them
at other objects, or letting the references themselves die --
makes the object garbage, and making it garbage sooner rather
than later might let it get collected on the next GC instead
of on the one after it. Thus the effective lifetime might
be shortened and the collector might therefore work a little
less hard -- but this is an effect so small it would be hard
to measure.)
A suggestion: If your compatriots are so convinced that
their method produces a performance improvement, challenge them
to measure the size of the improvement by actual experiment.
Run the code with the method as it stands and run it again with
the guts of the method commented out, and ask them how much
difference it made. My bet: If the measurement is done with
great care, the commented-out version will run just a hair
faster than the "optimized" original.
--
Eric.Sosman@sun.com