if d-tor is called, does it means the object is gc-ed?

Sean Kelly sean at f4.ca
Thu Oct 5 13:02:21 PDT 2006


%u wrote:
> Sorry for the stupid question, but I'm puzzled.
> 
> I have some code like this:
> 
> class A {
> 
> ~this() {
>   printf("d-tor\n");
> }
> 
> }
> 
> And I created lots of A's.  At some point in my program I explicitly did:
> delete a;  for all the a's.
> 
> And the printed msg shows the d-tor is called.  But From the system monitor, I
> didn't see the memory usage is decreased.  So I wonder whether the memory is
> actually freed.

Calling delete on an object will cause the GC to reclaim the memory used 
by that object and will call its dtor.  It is up to the GC, however, 
what happens to that memory.  Typically, it will be kept on hand for 
future allocations.

> BTW, is there other way I can enforce memory free-ing?

Calling delete is the only way to ensure an object is cleaned up, though 
the GC should detect nearly all unused objects in its collection run.

> I think the std.gc module should also provide some APIs like
> 
> ulong std.gc.allocatedMemory();  // in bytes
> ulong std.gc.availableMemory();
> 
> So we can monitor the memory usage within the program itself.

This certainly wouldn't be difficult to implement.


Sean



More information about the Digitalmars-d-learn mailing list