Garbage collector noob

Lutger lutger.blijdestijn at gmail.com
Wed Mar 7 04:09:29 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.
> 
> ---
> import std.gc;
> 
> void main()
> {
>     int[] a;
> 
>     for (;;) {
>         a = new int[1024 * 1024 * 10];
>         a = null;
>         assert(a.ptr is null);
>         fullCollect();
>     }
> }
> ---
> 
> 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?

Not being exactly an expert on garbage collection myself, I did 
encounter the same behaviour which could be changed with a call to 
std.gc.minimize. This led me to think fullCollect does in fact not 
always clean up all unused memory, but just forces a GC scan, which may 
decide there is no need to collect if enough memory is available. Can 
anybody confirm this?


More information about the Digitalmars-d-learn mailing list