Garbage Collection, Allocators/Deallocators and

Sean Kelly sean at invisibleduck.org
Sat Sep 18 07:08:59 PDT 2010


Ivo Kasiuk Wrote:

> Hi,
> 
> to improve my understanding of the GC and when/how
> allocators/deallocators and constructors/destructors get called, I wrote
> a little test program. And now I understand even less than before...
...
> Running this with DMD 2.049, I observed the following:
> 
> - S1(int), S2(int), C1(), C2(), S1.new and S2.new get invoked in every
> cycle of the loop, as you would expect.
> 
> - ~C2() also gets invoked frequently.
> 
> - ~S1(), ~S2(), ~C1(), S1.delete and C1.delete never ever get called.
> 
> - The program does not run out of memory and actually has a relatively
> modest memory footprint, so it does not seem to leak memory. (When using
> std.c.stdlib.malloc instead of GC.malloc it runs out of memory almost
> immediately).
> 
> It is good to see that the GC apparently really frees the unreferenced
> memory again. However, I don't understand why the deallocators and the
> destructors (apart from ~C2) do not get called. If the memory for the
> objects gets freed, as is apparently the case, then why are there no
> destructor and deallocator calls for these objects?

The deallocator is only called if you delete the object, not when it's finalized by the GC.  The GC will only finalize something that is in its memory space, so if this happens there's no need to call the deallocator.


More information about the Digitalmars-d-learn mailing list