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,
Thank you so much for this reply. This is exactly what I suspected,
and definitely what I wanted to hear.
I suspect this came about after someone discovered that connections
have to be closed to prevent memory leaks, and decided we needed to
'close' everything we can. I'm assuming that even:
ResultSet rs = ...
rs = null;
is pointless, since the collector will know to clean up rs after it is
no longer used?
The application this is being developed for is Sun Identity Manager,
so I can't really measure the performance of the difference, but the
logic you present above should be enough to convince people that this
is wrong, and start making changes.
Thanks again Eric, this is a big help. Now I just need to get people
to stop adding 'throws Exception' to the end of every method...