"pause" garbage collection in parallel code

Daniel Murphy via Digitalmars-d digitalmars-d at puremagic.com
Mon Dec 15 03:54:43 PST 2014


"Stephan Schiffels"  wrote in message 
news:wjeozpnitvhtxrkhulaz at forum.dlang.org...

> I see several ways how to improve my code:
> 1.) Is there a way to tell the GC the maximum heap size allowed before it 
> initiates a collection cycle? Cranking that up would cause fewer 
> collection cycles and hence spend more time in my multithreaded code?

Yes, sort of.  You can use 
http://dlang.org/phobos/core_memory.html#.GC.reserve to have the GC grab a 
big chunk of memory, and collections won't run until that is exhausted.  As 
it is only allocating virtual memory, it should be more or less equivalent 
to setting the max heap size.

> 2.) Is there a way to "pause" the GC collection for the parallel part of 
> my program, deliberately accepting higher memory usage?

Yes, with GC.enable and GC.diable

> 3.) Most of the memory is used in one huge array, perhaps I should simply 
> use malloc and free for that particular array to avoid the GC from running 
> so often.

Yes, this may work if it's a reasonable design for your application.  Other 
options like re-using one GC buffer per thread might work too.

> I am a bit surprised that there is no command line option for dmd to 
> control GC maximum heap size. Who determines how often the GC is run? For 
> example, in Haskell I can simply set the maximum heap size to 10Mb in GHC 
> using -A10m, which I used in the past to help exactly the same problem and 
> dramatically reduce the frequency of GC collection cycles.

Being able to configure the GC at run-time is something that's currently 
being worked on.  I assume this will be possible rather soon, probably in 
the next release.



More information about the Digitalmars-d mailing list