The purpose of D (GC rant, long)

Sean Kelly sean at f4.ca
Thu Oct 26 11:23:03 PDT 2006


Kyle Furlong wrote:
> 
> That said, isnt one able to disable the GC through std.gc.disable() or 
> some such call? What exactly does it do, now that I think about it? 

When memory is allocated from the GC it first looks to see if it has any 
available internally.  If it can't find any it runs a collection and 
looks again.  If it still can't find any then it obtains more memory 
from the OS and uses a portion of that for the allocation.  Setting 
gc.disable() prevents the collection run from occurring so the GC will 
simply obtain more memory from the OS when it runs out.  The reason 
behind this is that collections can take a long time, and 
performance-critical sections of code don't necessarily want to wait for 
a collection to occur just because they called 'new'.  So they'll either 
disable and re-enable the GC around critical sections of code or they'll 
simply keep the GC disabled and manually collect during idle periods 
using gc.fullCollect().

> Perhaps, for cases such as this, it would be helpful for there to be a 
> page on digitalmars.com/d/ which lists the language features that are gc 
> supported such that any prospective developers of the OP's bent can 
> easily avoid the allocations and memory waste they dont want to invoke.

Now that memory is not freed when string.length is set to zero it's 
quite possible to avoid most reallocations simply by preallocating in 
buffers before using them (ie. set length to some large number and then 
back to zero).  However, some operations will still cause an allocation, 
such as appending to a slice.


Sean



More information about the Digitalmars-d mailing list