Chris Kelly
Posts: 2
Nickname: bigkahuna
Registered: Aug, 2009
|
|
Re: GC - Thread safe
|
Posted: Aug 15, 2009 1:15 PM
|
|
As per my understanding the thread that runs the GC is a JVM/System thread.
But more importantly why do you care whether System.gc() is thread safe. If you call System.gc() all you're doing is signalling an intent to the JVM that garbage collection should be performed. Whether or when it happens is another matter.
You can think of System.gc() as an asynchronous call rather than a synchronous one. Just because System.gc() returns, it does not mean that a garbage collection has occured. It just means a request has been made for garbage collection.
Finally System.gc() eventually boils down to a native call so it is system dependent. So you can't really rely on its behaviour from platform to platform.
If you're using System.gc() so that finalize methods get called to clean up resources, then this smells of a bad design because you'll never know exactly when your finalizers are run. The use of finalizers are deemed v.bad practice these days.
|
|