[Gc] Re: Do finalizers run at exit?

Achilleas Margaritis axilmar at otenet.gr
Sat Dec 29 12:58:50 PST 2007


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).
> 
> --shiro
> 
> 
> 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
>> http://www.hpl.hp.com/hosted/linux/mail-archives/gc/
>>



More information about the Gc mailing list