The garbage collector
The GC has been a background presence throughout this book, occasionally coming to the fore when we discussed specific language and library features. For a large number of D programs, it is never necessary to interact directly with the GC. A programmer needs to be aware of when collection cycles may occur, how to write GC-free code with @nogc
, and how to avoid the potential issues that may arise when using GC-managed memory with C libraries, but the need to get down and dirty with the GC API is rare. When those times do arise, the core.memory
module comes into play.
In this module, you'll find a single structure, GC
, which contains a number of static
member functions. Automatic garbage collection can be turned off with GC.disable
. This doesn't turn off the GC completely—it will still run when the system is out of memory—but it does prevent it from running during normal usage. Collection can be forced by calling GC.collect
. There have been reports of significant performance...