View source code
Display the source code in core/memory.d from which this page was generated on
github.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Page wiki
View or edit the community-maintained wiki page associated with this page.
Module core.memory
This module provides an interface to the garbage collector used by applications written in the D programming language. It allows the garbage collector in the runtime to be swapped without affecting binary compatibility of applications.
Using this module is not necessary in typical D code. It is mostly useful when doing low-level memory management.
Notes to users
- The
GCis a conservative mark-and-sweep collector. It only runs a collection cycle when an allocation is requested of it, never otherwise. Hence, if the program is not doing allocations, there will be noGCcollection pauses. The pauses occur because all threads theGCknows about are halted so the threads' stacks and registers can be scanned for references toGCallocated data. - The
GCdoes not know about threads that were created by directly calling the OS/C runtime thread creation APIs and D threads that were detached from the D runtime after creation. Such threads will not be paused for aGCcollection, and theGCmight not detect references toGCallocated data held by them. This can cause memory corruption. There are several ways to resolve this issue:- Do not hold references to
GCallocated data in such threads. - Register/unregister such data with calls to addRoot/removeRoot and addRange/removeRange.
- Maintain another reference to that same data in another thread that the
GCdoes know about. - Disable
GCcollection cycles while that thread is active with disable/enable. - Register the thread with the
GCusingcore.thread.thread_attachThis/core.thread.thread_detachThis.
- Do not hold references to
Notes to implementors
- On POSIX systems, the signals SIGUSR1 and SIGUSR2 are reserved by this module for use in the garbage collector implementation. Typically, they will be used to stop and resume other threads when performing a collection, but an implementation may choose not to use this mechanism (or not stop the world at all, in the case of concurrent garbage collectors).
- Registers, the stack, and any other memory locations added through
the
function are always scanned conservatively. This means that even if a variable is e.g. of typeGC.addRangefloat, it will still be scanned for possibleGCpointers. And, if the word-interpreted representation of the variable matches aGC-managed memory block's address, that memory block is considered live. - Implementations are free to scan the non-root heap in a precise
manner, so that fields of types like
floatwill not be considered relevant when scanning the heap. Thus, casting aGCpointer to an integral type (e.g.size_t) and storing it in a field of that type inside theGCheap may mean that it will not be recognized if the memory block was allocated with precise type info or with theattribute.GC.BlkAttr.NO_SCAN - Destructors will always be executed while other threads are active; that is, an implementation that stops the world must not execute destructors until the world has been resumed.
- A destructor of an
objectmust not accessobjectreferences within theobject. This means that an implementation is free to optimize based on this rule. - An implementation is free to perform heap compaction and copying
so long as no valid
GCpointers are invalidated in the process. However, memory allocated withmust not be moved/copied.GC.BlkAttr.NO_MOVE - Implementations must support interior pointers. That is, if the
only reference to a
GC-managed memory block points into the middle of the block rather than the beginning (for example), theGCmust consider the memory block live. The exception to this rule is when a memory block is allocated with theattribute; it is the user's responsibility to make sure such memory blocks have a proper pointer to them when they should be considered live.GC.BlkAttr.NO_INTERIOR - It is acceptable for an implementation to store bit flags into
pointer values and
GC-managed memory blocks, so long as such a trick is not visible to the application. In practice, this means that only a stop-the-world collector can do this. - Implementations are free to assume that
GCpointers are only stored on word boundaries. Unaligned pointers may be ignored entirely. - Implementations are free to run collections at any point. It is, however, recommendable to only do so when an allocation attempt happens and there is insufficient memory available.
Structs
| Name | Description |
|---|---|
GC
|
This struct encapsulates all garbage collection functionality for the D programming language. |
Authors
Sean Kelly, Alex Rønne Petersen