Re: Garbage collection
Crouchez wrote:
When exactly does it run? Is it a scheduled thing or when memory hits a
certain point? And what effect does manually calling System.gc() have on the
process?
Different JVM's have different policies about when and how
to carry out garbage collection. So the answer to "exactly
when" is "whenever it likes," and the answer to "is it scheduled
or is it triggered" is "yes." All you can truly rely on is that
the JVM will not throw OutOfMemoryError without attempting a
garbage collection first.
System.gc() does what its Javadoc says: It "suggests" that
the JVM collect some garbage, and the JVM will make a "best
effort" to do so before the method returns. However, there's no
precise definition of what a "best effort" is: If the JVM decides
that a garbage collection would be inconvenient at the moment,
its "best effort" might amount to ignoring the "suggestion."
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.
--
Eric Sosman
esosman@ieee-dot-org.invalid