Re: Garbage collection
Eric Sosman wrote:
Mike Schilling wrote:
Eric Sosman wrote:
The prevailing wisdom is that System.gc() is very seldom a
good idea, particularly in code that is expected to run on a
variety of different JVM implementations with different garbage
collectors. In limited circumstances when the code is running on
one well-researched version of one specific JVM it may make sense,
but that's about it. Calling System.gc() is, in essence, making
a static decision based on predicted conditions instead of letting
the JVM make a dynamic decision based on actual conditions. You
may be smarter than the JVM, but the JVM has much more information
than you do.
As always, there are exceptions. Say you have a program that:
1. Interacts with the user
2. When asked to, runs some time and memory-intensive processing
3. Interacts with the user some more
It makes sense to call System.gc() at the end of step 2, so that
the
time required for the GC doesn't interrupt the user interaction.
Note that I wrote "very seldom," not "never."
You, I think, should have written "may make sense" instead
of "makes sense." The fact that Step 2 is time-intensive is
irrelevant,
Not at all. If the user is already expecting to wait for the
processing to complete, the added wait for GC becomes invisible, which
is the point.
and the fact that it is memory-intensive matters
only if (1) a lot of garbage has accumulated and (2) a collection
is imminent anyhow.
The point is that, the wait at the end of 2 being invisible to the
user, it's worth doint to avoid a visible wait later. An insurance
premium, if you like.