Disabling GC in D

Chris Wright via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 21 14:15:13 PST 2016


On Thu, 21 Jan 2016 21:54:36 +0000, Dibyendu Majumdar wrote:

> Is there a way to disable GC in D?
> I am aware of the @nogc qualifier but I would like to completely disable
> GC for the whole app/library.
> 
> Regards Dibyendu

In order to suppress GC collections, you can call core.memory.GC.disable. 
This doesn't prevent you from allocating GC memory. You will have a 
memory leak if you use the GC at all.

Alternatively, if you never allocate GC memory, the GC will never run.

Finally, you can use gc_setProxy() with a a GC proxy you create. Have it 
throw an exception instead of allocating. That means you will get crashes 
instead of memory leaks if something uses the GC when it shouldn't.

gc_setProxy() and struct Proxy seem not to be part of the public runtime. 
You can copy the definitions into your own project -- they're listed as 
extern(C) to make that easier. This may tie you to specific DMD revisions 
in the case that the GC interface changes.


More information about the Digitalmars-d-learn mailing list