[Gc] Do finalizers run at exit?

Bruce Hoult bruce at hoult.org
Tue Dec 25 12:03:13 PST 2007

On Dec 26, 2007 8:43 AM, Achilleas Margaritis <axilmar at otenet.gr> wrote:
> Do finalizers run at exit, in version 7.0? although my finalizers
> register correctly, they are not invoked at exit.

You know .. there is a ton of documentation and papers written by Hans
out there.  You could try reading them:


In short: running finalizers at program exit is the *wrong* thing to do.

The fundamental difficulty is that the GC will finalizer objects only
in a dependency-safe order, but end of program cleanup has to run all
cleanup actions immediately and thus has to ignore dependencies.  Thus
finalization and cleanup are totally different things.

What you should do is have your own mechanism to clean up your objects
at program exit.  If one of those objects is garbage collected first
then the finalizer needs to remove it from the list of objects to be
cleaned up.  And be *extremely* careful of race conditions.
Finalizers are hard, especially ones run synchronously by the GC.  We
have half a dozen finalizers in our system and I think every single
one of them has turned out to have a race condition bug in various
ways, often after months of active use in the field.  They are VERY
VERY hard to track down.

More information about the Gc mailing list