www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - if d-tor is called, does it means the object is gc-ed?

reply %u <no where.com> writes:
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.

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

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.
Oct 05 2006
next sibling parent reply Sean Kelly <sean f4.ca> writes:
%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
Oct 05 2006
parent "Unknown W. Brackets" <unknown simplemachines.org> writes:
You can already access this information using, iirc, std.gc.getStats(). 
  Just pull the usedsize and poolsize (used and allocated memory, 
respectively.)

-[Unknown]


 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.
Oct 06 2006
prev sibling parent "Lionello Lunesu" <lionello lunesu.remove.com> writes:
"%u" <no where.com> wrote in message news:eg3lg4$123$1 digitaldaemon.com...
 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.
You shouldn't worry about the memory usage shown in system monitor. The memory usage in the windows task manager, for example, is reserved Virtual Memory. This doesn't actually mean anything, since an app can reserve virtual memory as much as it wants, without consuming even a bit of physical memory. Like Sean said, your objects will have been freed by the GC, but the virtual memory won't be freed, and why should it be freed: it doesn't actually cost anything. L.
Oct 05 2006