Garbage collector noob

Sean Kelly sean at f4.ca
Tue Mar 6 23:36:57 PST 2007


torhu wrote:
> I haven't dealt directly with the garbage collector much before, but 
> just used delete and scope to deal with deallocation.  And left some of 
> the minor allocations to be freed by the GC.
> 
> Now I want to work more closely with the GC.  Can anyone explain to me 
> why fullCollect doesn't seem to do anything here?  This example just 
> keeps allocating memory, until the GC seems to kick in to stop it from 
> growing.  It stabilizes at 426MB.  The same example using Tango instead 
> stops at 597MB.

When I run the test on Tango, memory juse jumps to 467k and stays steady 
there.  Doing some quick math with a calculator shows an array 1024^2*10 
integers should occupy ~409k of memory, so the rest is just program 
overhead.  For what it's worth, running this app on Tango without the 
call to gc.collect() brings memory use up to 594k.  I'd have to debug 
the GC to figure out why.

> Removing the call to fullCollect() doesn't change the behavior.  I 
> expected fullCollect to collect as much memory as it can, and then reuse 
> it for the next allocation.  Am I on the wrong track here?

Nope.  That's what it should do.  But be aware that the current GC 
allocates memory in pages and those pages are devoted to a specific size 
of data.  And once so assigned, I don't think the GC ever attempts to 
reclaim empty pages if another size allocation is required.  It doesn't 
matter in this case because anything over a page (4k) is the same "size" 
as far as the GC is concerned, but it may matter in an app that 
allocates a ton of small objects and discards them, then allocates a ton 
of larger objects and discards them, etc.


Sean


More information about the Digitalmars-d-learn mailing list