A collection of DIPs

Adam D. Ruppe via Digitalmars-d digitalmars-d at puremagic.com
Sat Sep 12 08:54:46 PDT 2015


On Saturday, 12 September 2015 at 06:25:45 UTC, NX wrote:
> Is there even a compiler switch to disable GC altogether so the 
> program doesn't have a GC thread? No, I can't see it...

D programs *never* have a GC thread.

The way the D garbage collector works is something like this:

void* GC.allocate(size) {
    if(!GC.disabled && existing_memory_usage + size >= 
existing_gc_pool) {
         GC.collect();
         if(existing_memory_usage + size >= existing_gc_pool) {
            request_more_memory_from_operating_system();
         }
    }
    if(still_not_enough_room) throw OutOfMemoryError();

    existing_memory_usage += size;
    return pointer;
}


The GC only collects when GC.collect is called, which runs as a 
normal function in the current thread (it pauses other threads it 
is aware of so they don't cause race conditions btw). GC.collect 
is only called when you explicitly ask for it by calling it 
yourself, at program termination, or when you call allocate as 
seen in the pseudocode above.


If you never call GC.allocate, the GC never actually does 
anything. It will never pause the program and it will never 
collect.


That's what @nogc does btw, it doesn't disable the GC since 
there's nothing to disable, it just flags any call to GC.allocate 
(including hidden ones behind the new expression, etc) as a 
compile error.



Remember, the D GC isn't magic and isn't actually even that 
complicated, it is just an ordinary function call.


More information about the Digitalmars-d mailing list