[Gc] Re: Do finalizers run at exit?
Hans.Boehm at hp.com
Sat Dec 29 19:48:58 PST 2007
A slightly older version of the ACM POPL paper is available at
The very short answer is that to run finalizers at exit, you need to
finalize objects that are still reachable, including those that are
reachable from global variables and static data members. Once you've
finalized the first such object, it is very unclear what library
routines still work correctly. Thus it is unclear what subsequently
run finalizers could safely do, and it wouldn't be much. If you
supported this, all finalizers would have to be prepared to run in
an environment in which the libraries they really on are partially or
completely broken. I don't think this is a reasonable restriction.
The above paper gives a way to safely program around this restriction.
This is very different from repeatedly running collections until as many
objects as possible are finalized. That is safe.
On Sat, 29 Dec 2007, Achilleas Margaritis wrote:
> Thanks a lot for replying, but my question is still unanswered.
> It's not a difficult question, is it? I am just asking why finalizers
> are NOT invoked AT EXIT, while they are INVOKED after a COLLECTION.
> At exit, there is no need to revive objects, as the application is
> exiting. You just call the finalizers of all remaining objects.
> If a destructor happens to allocate memory (highly impropable, but
> possible), then the collector might delay exiting a little until the
> newly allocated memory is no longer used. It's acceptable, as it is a
> design flaw to do such an action anyway in a finalizer.
> O/H Shiro Kawai ??????:
>> I haven't read the paper, but I have used finalizers of Bohem GC,
>> and here's my take (based on my understanding of the code; please
>> correct me if I'm wrong).
>> It is _not_ guaranteed that an object's finzalier is called
>> immediately after the collecting cycle in which the object become
>> unreachable. After all live objects are marked normally, GC scans
>> finalizable objects and marks objects pointed by one or more
>> of them(*), then run finalizers on objects that are still
>> unmarked. Suppose you have object A pointing object B, both
>> having finalizers, and both become unreachable at one cycle,
>> B is still marked and only A's finalizer runs just after that
>> cycle. If A's finalizer doesn't insert a pointer to B into a
>> root set, B is still unreachable after this cycle, and it will
>> be collected and its finalizer will run in the _next_ cycle.
>> Obviously, if you have a very long chain of finalizable objects,
>> it will take many GC cycles to call all finalizers.
>> (* If finalizer is registered as NO_ORDER flag no objects from
>> finalizable objects are marked. But you have to take a risk that
>> finalizer will see objects that are already finalized.)
>> During program execution, such delay is hidden and only
>> observable as increased average time of objects being collected,
>> but if all finalizers needs to be invoked upon program exit,
>> it may wait long time (or worse, it won't stop).
>> There is a function (at least in 6.8), GC_finalize_all, that
>> repeatedly invokes finalizers and queues finalizable objects
>> until all finalizers are run. If you desperately need to run
>> all finalizers upon program exit, probably you can set up
>> atexit handler and call it. It is only defined if GC is
>> configured with JAVA_FINALIZATION I assume, and declared in
>> gc/include/javaxfc.h in 6.8. Read the comment in the header
>> for the reason it is not a public API.
>> (GC developers may have different recommendation---this is only
>> what I found as a user of the library).
>> From: Achilleas Margaritis <axilmar at otenet.gr>
>> Subject: [Gc] Re: Do finalizers run at exit?
>> Date: Sat, 29 Dec 2007 17:01:39 +0200
>>> If you mean the ACM paper, I can not download it with the basic ACM subscription. According to the site, I have to purchase a subscription.
>>> Since I am not going to do that, could someone please explain to me why finalizers can run after a collection, but not on program exit?
>>> O/H Bruce Hoult ??????:
>>>> On Dec 30, 2007 12:22 AM, Achilleas Margaritis <axilmar at otenet.gr> wrote:
>>>>> The question still remains:
>>>>> Why finalizers are run after a collection and not on program exit?
>>>> You have already been pointed in the direction of a paper that
>>>> explains this more than adequately.
>>> Gc mailing list
>>> Gc at linux.hpl.hp.com
> Gc mailing list
> Gc at linux.hpl.hp.com
More information about the Gc