The aside (was: Crash when running 1.6.0_03)

Nicklas Johnson freebsd at spatula.net
Fri Aug 15 21:27:12 UTC 2008


There are many reasons not to call System.gc():

1. It only "suggests" that the GC do collection, and can be completely
ignored... so if you're relying on some behaviour out of gc(), you're likely
to see unexplained and unpredictable breakage across platforms.
2. Modern GCs are clever enough to know when is a good time to collect
garbage; calling System.gc is more likely to reduce performance versus just
letting the GC do its job.
3. On some platforms (like Sun's JVM), System.gc() will result in a *major*
collection happening, which is probably more than what you want (and usually
an order of magnitude slower).

You're far better off *tuning* the garbage collector and heap sizes rather
than explicitly trying to invoke the garbage collector.

See also:

http://java.sun.com/docs/hotspot/gc/index.html
http://java.sun.com/docs/hotspot/HotSpotFAQ.html#gc_pooling
http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html#1.1.Other%20Considerations|outline

2008/8/15 Kees Jan Koster <kjkoster at kjkoster.org>

> Dear Nicklas,
>
>  As an aside, it's a really, really bad idea to ever call System.gc()
>> directly in a modern JVM.  It's much better to leave it to the JVM to
>> decide
>> when to do garbage collection.
>>
>
> Interesting. Could you elaborate on this? What makes doing a manual gc() so
> bad? Any documents on the 'web that talk about this in detail?
>
> --
> Kees Jan
>
> http://java-monitor.com/forum/
> kjkoster at kjkoster.org
> 06-51838192
>
> Rule 1 for being in a hole: stop digging.
>
>


-- 
"Courage isn't just a matter of not being frightened, you know. It's being
afraid and doing what you have to do anyway."
-- Doctor Who - Planet of the Daleks
This message has been brought to you by Nick Johnson 2.3b1 and the number 6.
http://healerNick.com/ http://morons.org/ http://spatula.net/


More information about the freebsd-java mailing list