Let me add that there is
one and only one guarantee you get about garbage collection, and that is that it will run before the JVM throws an OutOfMemoryError at you.
This means that the only valid reason to call System.gc() is that you know there's lots of garbage on the heap[1], you have a natural "lull" in your application,
and not collecting garbage at that point will impact performance shortly after. Even so, you don't get any guarantees that the garbage collector will run.
- Peter
[1] With modern generational garbage collectors, I think this mainly happens when you release lots of objects that you've held on to for a fairly long time. Objects with a short lifespan live on a kindergarten heap which is garbage collected very aggressively. If you're interested, you might enjoy
this article.
[ February 15, 2003: Message edited by: Peter den Haan ]