New in D

Mindy (0xEAB) desisma at heidel.beer
Wed Jul 15 02:07:56 UTC 2026


On Wednesday, 15 July 2026 at 01:54:28 UTC, Jakepys wrote:
> although I don't think it's enabled by default.

The GC is semi-opt-out.
It only ever collects memory when you ask it to allocate memory 
for you or you call its `collect()` function.

It also features a `disable()` function, that bumps the internal 
counter and prevents collections from occurring. The counterpart 
is `enable()` which counterbumps the counter. The counter allows 
you to focus on your code without worrying about re-enabling the 
collection mechanism early.

You can also register your own memory segments with the GC, so 
that the GC can scan those for pointers into GC-allocated memory.

Here's the corresponding library documentation:
<https://dlang.org/library/core/memory/gc.html>

As you might be able to tell, the GC is part of Druntime. If you 
choose to opt out of using the runtime (e.g. by passing 
`-betterC` to the compiler), you'll not have it available.

Further information on the GC's inner workings can be found here:
<https://dlang.org/library/core/memory.html>

Last but not least, if you don't mind the slop “cartoon” graphic 
and messed up document layout, you might find the text of this 
spec page interesting: <https://dlang.org/spec/garbage.html>


More information about the Digitalmars-d-learn mailing list