GC object finalization not guaranteed

Fawzi Mohamed fmohamed at mac.com
Sat Apr 18 14:41:07 PDT 2009


On 2009-04-18 23:03:11 +0200, Leandro Lucarella <llucax at gmail.com> said:

> Fawzi Mohamed, el 18 de abril a las 22:48 me escribiste:
>> On 2009-04-18 22:25:32 +0200, Don <nospam at nospam.com> said:
>> 
>>> Leandro Lucarella wrote:
>>>> Robert Jacques, el 18 de abril a las 11:56 me escribiste:
>>>>> On Sat, 18 Apr 2009 11:24:14 -0400, Leandro Lucarella <llucax at gmail.com> wrote:
>>>>>> I've just found out[1] this[2]:
>>>>>> 	The garbage collector is not guaranteed to run the destructor for
>>>>>> 	all unreferenced objects.
>>>>>> Is there any reason why D can't guarantee that all finalizers will be
>>>>>> called, at least when the program ends?
>> 
>> when the main thread ends other threads might be running, and removing
>> memory they are using is not necessarily a good idea.
> 
> Then I guess gc_term() should be invoked when all threads have terminated.
> Is there any technical difficulty to do that?

well killing a daemon thread might not be so easy, the cleanest thing 
would be to simulate an exception into it and unwind the stack.
If you are not careful you might kill a thread when it did acquire a 
lock that might deadlock your own thread.

>> Exceptions might delay things indefinitely and allocate more memory.
> 
> Then I guess gc_term() should be invoked when all exceptions were
> processed. Is there any technical difficulty to do that?

this should be sort of doable (without having looked at the details)

> I really didn't take a look at the compiler runtime stuff, so I don't
> really know how all that works.
> 
>> Actually with tango you can tweak what will happen with
>> gc_getTermCleanupLevel (in gc.d), but I don't think that there is
>> a satisfying solution for everybody.
> 
> Yes, I saw that (I commented about that in the blog post), but there is
> still no option to run the finalizers to all the live objects.

well you could try to ignore all roots but the gc ones (you don't want 
to deallocate it before you have finished using it :).

> Anyways, I wonder why it's not part of the GC specs in druntime.
> 
>> I think that the default in tango is quite reasonable (normal collect).
> 
> Yes... With the current specs at least, I guess.
> 
> I think that if finalization is not guarantee, at least at
> program exit, there shouldn't be there. It's useless.

not true, there are still cases in which it is useful (weak, ref, and 
what Robert Jaques said.
Actually one of the first comment when I added gc_setTermCleanupLevel, 
was nice so now my program can finish faster, not everybody wants to 
wait too long after the program has finished doing useful stuff for it 
to die.

> Finalization should be possible only for scope classes if no finalization
> guarantee can be provided by the GC. It's useless and error prone
> (introducing a bug very difficult to track). Who wants randomly executed
> destructors?

destructors are executed in a randmized order (so you might consume an 
external resource if it is limited). That they might not be executed at 
all, is only a small extra problem. Indeed they are less useful than 
(for example) C++, but can still have some uses.

Fawzi




More information about the Digitalmars-d mailing list