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.
addRoot
/removeRoot
and addRange
/removeRange
.disable
/enable
.core.thread.thread_attachThis
/core.thread.thread_detachThis
.GC.addRange
function are always scanned conservatively. This means that even if a variable is e.g. of type float
, it will still be scanned for possible GC pointers. And, if the word-interpreted representation of the variable matches a GC-managed memory block's address, that memory block is considered live.float
will not be considered relevant when scanning the heap. Thus, casting a GC pointer to an integral type (e.g. size_t
) and storing it in a field of that type inside the GC heap may mean that it will not be recognized if the memory block was allocated with precise type info or with the GC.BlkAttr.NO_SCAN
attribute.GC.BlkAttr.NO_MOVE
must not be moved/copied.GC.BlkAttr.NO_INTERIOR
attribute; it is the user's responsibility to make sure such memory blocks have a proper pointer to them when they should be considered live.This struct encapsulates all garbage collection functionality for the D programming language.
Aggregation of GC stats to be exposed via public API
number of used bytes on the GC heap (might only get updated after a collection)
number of free bytes on the GC heap (might only get updated after a collection)
number of bytes allocated for current thread since program start
Aggregation of current profile information
total number of GC cycles
total time spent doing GC
total time threads were paused doing GC
largest time threads were paused during one GC cycle
largest time spent doing one GC cycle
Enables automatic garbage collection behavior if collections have previously been suspended by a call to disable. This function is reentrant, and must be called once for every call to disable before automatic collections are enabled.
Disables automatic garbage collections performed to minimize the process footprint. Collections may continue to occur in instances where the implementation deems necessary for correct program behavior, such as during an out of memory condition. This function is reentrant, but enable must be called once for each call to disable.
Begins a full collection. While the meaning of this may change based on the garbage collector implementation, typical behavior is to scan all stack segments for roots, mark accessible memory blocks as alive, and then to reclaim free space. This action may need to suspend all running threads for at least part of the collection process.
Indicates that the managed memory space be minimized by returning free physical memory to the operating system. The amount of free memory returned depends on the allocator design and on program behavior.
Elements for a bit field representing memory block attributes. These are manipulated via the getAttr, setAttr, clrAttr functions.
No attributes set.
Finalize the data in this block on collect.
Do not scan through this block on collect.
Do not move this memory block on collect.
This block contains the info to allow appending.
This can be used to manually allocate arrays. Initial slice size is 0.
capacity
to retrieve actual usable capacity. // Allocate the underlying array. int* pToArray = cast(int*)GC.malloc(10 * int.sizeof, GC.BlkAttr.NO_SCAN | GC.BlkAttr.APPENDABLE); // Bind a slice. Check the slice has capacity information. int[] slice = pToArray[0 .. 0]; assert(capacity(slice) > 0); // Appending to the slice will not relocate it. slice.length = 5; slice ~= 1; assert(slice.ptr == p);
This block is guaranteed to have a pointer to its base while it is alive. Interior pointers can be safely ignored. This attribute is useful for eliminating false pointers in very large data structures and is only implemented for data structures at least a page in size.
Contains aggregate information about a block of managed memory. The purpose of this struct is to support a more efficient query style in instances where detailed information is needed.
base = A pointer to the base of the block in question. size = The size of the block, calculated from base. attr = Attribute bits set on the memory block.
Returns a bit field representing all block attributes set for the memory referenced by p. If p references memory not originally allocated by this garbage collector, points to the interior of a memory block, or if p is null, zero will be returned.
void* p
| A pointer to the root of a valid memory block or to null. |
Sets the specified bits for the memory references by p. If p references memory not originally allocated by this garbage collector, points to the interior of a memory block, or if p is null, no action will be performed.
void* p
| A pointer to the root of a valid memory block or to null. |
uint a
| A bit field containing any bits to set for this memory block. |
Clears the specified bits for the memory references by p. If p references memory not originally allocated by this garbage collector, points to the interior of a memory block, or if p is null, no action will be performed.
void* p
| A pointer to the root of a valid memory block or to null. |
uint a
| A bit field containing any bits to clear for this memory block. |
Requests an aligned block of managed memory from the garbage collector. This memory may be deleted at will with a call to free, or it may be discarded and cleaned up automatically during a collection run. If allocation fails, this function will call onOutOfMemory which is expected to throw an OutOfMemoryError.
size_t sz
| The desired allocation size in bytes. |
uint ba
| A bitmask of the attributes to set on this block. |
TypeInfo ti
| TypeInfo to describe the memory. The GC might use this information to improve scanning for pointers or to call finalizers. |
Requests an aligned block of managed memory from the garbage collector. This memory may be deleted at will with a call to free, or it may be discarded and cleaned up automatically during a collection run. If allocation fails, this function will call onOutOfMemory which is expected to throw an OutOfMemoryError.
size_t sz
| The desired allocation size in bytes. |
uint ba
| A bitmask of the attributes to set on this block. |
TypeInfo ti
| TypeInfo to describe the memory. The GC might use this information to improve scanning for pointers or to call finalizers. |
Requests an aligned block of managed memory from the garbage collector, which is initialized with all bits set to zero. This memory may be deleted at will with a call to free, or it may be discarded and cleaned up automatically during a collection run. If allocation fails, this function will call onOutOfMemory which is expected to throw an OutOfMemoryError.
size_t sz
| The desired allocation size in bytes. |
uint ba
| A bitmask of the attributes to set on this block. |
TypeInfo ti
| TypeInfo to describe the memory. The GC might use this information to improve scanning for pointers or to call finalizers. |
Extend, shrink or allocate a new block of memory keeping the contents of an existing block
If sz
is zero, the memory referenced by p will be deallocated as if by a call to free
. If p
is null
, new memory will be allocated via malloc
. If p
is pointing to memory not allocated from the GC or to the interior of an allocated memory block, no operation is performed and null is returned.
Otherwise, a new memory block of size sz
will be allocated as if by a call to malloc
, or the implementation may instead resize or shrink the memory block in place. The contents of the new memory block will be the same as the contents of the old memory block, up to the lesser of the new and old sizes.
The caller guarantees that there are no other live pointers to the passed memory block, still it might not be freed immediately by realloc
. The garbage collector can reclaim the memory block in a later collection if it is unused. If allocation fails, this function will throw an OutOfMemoryError
.
If ba
is zero (the default) the attributes of the existing memory will be used for an allocation. If ba
is not zero and no new memory is allocated, the bits in ba will replace those of the current memory block.
void* p
| A pointer to the base of a valid memory block or to null . |
size_t sz
| The desired allocation size in bytes. |
uint ba
| A bitmask of the BlkAttr attributes to set on this block. |
TypeInfo ti
| TypeInfo to describe the memory. The GC might use this information to improve scanning for pointers or to call finalizers. |
null
if sz
is zero or the pointer does not point to the base of an GC allocated memory block. OutOfMemoryError
on allocation failure.enum size1 = 1 << 11 + 1; // page in large object pool enum size2 = 1 << 22 + 1; // larger than large object pool size auto data1 = cast(ubyte*)GC.calloc(size1); auto data2 = cast(ubyte*)GC.realloc(data1, size2); GC.BlkInfo info = GC.query(data2); assert(info.size >= size2);
Requests that the managed memory block referenced by p be extended in place by at least mx bytes, with a desired extension of sz bytes. If an extension of the required size is not possible or if p references memory not originally allocated by this garbage collector, no action will be taken.
void* p
| A pointer to the root of a valid memory block or to null. |
size_t mx
| The minimum extension size in bytes. |
size_t sz
| The desired extension size in bytes. |
TypeInfo ti
| TypeInfo to describe the full memory block. The GC might use this information to improve scanning for pointers or to call finalizers. |
APPENDABLE
info). However, use the return value only as an indicator of success. capacity
should be used to retrieve actual usable slice capacity.size_t size = 1000; int* p = cast(int*)GC.malloc(size * int.sizeof, GC.BlkAttr.NO_SCAN); //Try to extend the allocated data by 1000 elements, preferred 2000. size_t u = GC.extend(p, 1000 * int.sizeof, 2000 * int.sizeof); if (u != 0) size = u / int.sizeof;
int[] slice = new int[](1000); int* p = slice.ptr; //Check we have access to capacity before attempting the extend if (slice.capacity) { //Try to extend slice by 1000 elements, preferred 2000. size_t u = GC.extend(p, 1000 * int.sizeof, 2000 * int.sizeof); if (u != 0) { slice.length = slice.capacity; assert(slice.length >= 2000); } }
Requests that at least sz bytes of memory be obtained from the operating system and marked as free.
size_t sz
| The desired size in bytes. |
Deallocates the memory referenced by p. If p is null, no action occurs. If p references memory not originally allocated by this garbage collector, if p points to the interior of a memory block, or if this method is called from a finalizer, no action will be taken. The block will not be finalized regardless of whether the FINALIZE attribute is set. If finalization is desired, call destroy
prior to GC.free
.
void* p
| A pointer to the root of a valid memory block or to null. |
Returns the base address of the memory block containing p. This value is useful to determine whether p is an interior pointer, and the result may be passed to routines such as sizeOf which may otherwise fail. If p references memory not originally allocated by this garbage collector, if p is null, or if the garbage collector does not support this operation, null will be returned.
inout(void)* p
| A pointer to the root or the interior of a valid memory block or to null. |
Returns the true size of the memory block referenced by p. This value represents the maximum number of bytes for which a call to realloc may resize the existing block in place. If p references memory not originally allocated by this garbage collector, points to the interior of a memory block, or if p is null, zero will be returned.
void* p
| A pointer to the root of a valid memory block or to null. |
Returns aggregate information about the memory block containing p. If p references memory not originally allocated by this garbage collector, if p is null, or if the garbage collector does not support this operation, BlkInfo.init will be returned. Typically, support for this operation is dependent on support for addrOf.
void* p
| A pointer to the root or the interior of a valid memory block or to null. |
Returns runtime stats for currently active GC implementation See core.memory.GC.Stats
for list of available metrics.
Returns runtime profile stats for currently active GC implementation See core.memory.GC.ProfileStats
for list of available metrics.
Adds an internal root pointing to the GC memory block referenced by p. As a result, the block referenced by p itself and any blocks accessible via it will be considered live until the root is removed again.
If p is null, no operation is performed.
void* p
| A pointer into a GC-managed memory block or null. |
// Typical C-style callback mechanism; the passed function // is invoked with the user-supplied context pointer at a // later point. extern(C) void addCallback(void function(void*), void*); // Allocate an object on the GC heap (this would usually be // some application-specific context data). auto context = new Object; // Make sure that it is not collected even if it is no // longer referenced from D code (stack, GC heap, …). GC.addRoot(cast(void*)context); // Also ensure that a moving collector does not relocate // the object. GC.setAttr(cast(void*)context, GC.BlkAttr.NO_MOVE); // Now context can be safely passed to the C library. addCallback(&myHandler, cast(void*)context); extern(C) void myHandler(void* ctx) { // Assuming that the callback is invoked only once, the // added root can be removed again now to allow the GC // to collect it later. GC.removeRoot(ctx); GC.clrAttr(ctx, GC.BlkAttr.NO_MOVE); auto context = cast(Object)ctx; // Use context here… }
Removes the memory block referenced by p from an internal list of roots to be scanned during a collection. If p is null or is not a value previously passed to addRoot() then no operation is performed.
void* p
| A pointer into a GC-managed memory block or null. |
Adds p[0 .. sz]
to the list of memory ranges to be scanned for pointers during a collection. If p is null, no operation is performed.
Note that p[0 .. sz]
is treated as an opaque range of memory assumed to be suitably managed by the caller. In particular, if p points into a GC-managed memory block, addRange does not mark this block as live.
void* p
| A pointer to a valid memory address or to null. |
size_t sz
| The size in bytes of the block to add. If sz is zero then the no operation will occur. If p is null then sz must be zero. |
TypeInfo ti
| TypeInfo to describe the memory. The GC might use this information to improve scanning for pointers or to call finalizers |
// Allocate a piece of memory on the C heap. enum size = 1_000; auto rawMemory = core.stdc.stdlib.malloc(size); // Add it as a GC range. GC.addRange(rawMemory, size); // Now, pointers to GC-managed memory stored in // rawMemory will be recognized on collection.
Removes the memory range starting at p from an internal list of ranges to be scanned during a collection. If p is null or does not represent a value previously passed to addRange() then no operation is performed.
void* p
| A pointer to a valid memory address or to null. |
Runs any finalizer that is located in address range of the given code segment. This is used before unloading shared libraries. All matching objects which have a finalizer in this code segment are assumed to be dead, using them while or after calling this method has undefined behavior.
void[] segment
| address range of a code segment. |
Pure variants of C's memory allocation functions malloc
, calloc
, and realloc
and deallocation function free
.
UNIX 98 requires that errno be set to ENOMEM upon failure. Purity is achieved by saving and restoring the value of errno
, thus behaving as if it were never changed.
ubyte[] fun(size_t n) pure { void* p = pureMalloc(n); p !is null || n == 0 || assert(0); scope(failure) p = pureRealloc(p, 0); p = pureRealloc(p, n *= 2); p !is null || n == 0 || assert(0); return cast(ubyte[]) p[0 .. n]; } auto buf = fun(100); assert(buf.length == 200); pureFree(buf.ptr);
Destroys and then deallocates an object.
In detail, __delete(x)
returns with no effect if x
is null
. Otherwise, it performs the following actions in sequence:
~this()
for the object referred to by x
(if x
is a class or interface reference) or for the object pointed to by x
(if x
is a pointer to a struct
). Arrays of structs call the destructor, if defined, for each element in the array. If no destructor is defined, this step has no effect. x
. If x
is a reference to a class or interface, the memory allocated for the underlying instance is freed. If x
is a pointer, the memory allocated for the pointed-to object is freed. If x
is a built-in array, the memory allocated for the array is freed. If x
does not refer to memory previously allocated with new
(or the lower-level equivalents in the GC API), the behavior is undefined. x
is set to null
. Any attempt to read or write the freed memory via other references will result in undefined behavior. destroy
to explicitly finalize objects, and only resort to core.memory._delete
when object.destroy
wouldn't be a feasible option. T x
| aggregate object that should be destroyed |
destroy
, core.GC.free
delete
keyword allowed to free GC-allocated memory. As this is inherently not @safe
, it has been deprecated. This function has been added to provide an easy transition from delete
. It performs the same functionality as the former delete
keyword.bool dtorCalled; class B { int test; ~this() { dtorCalled = true; } } B b = new B(); B a = b; b.test = 10; assert(GC.addrOf(cast(void*) b) != null); __delete(b); assert(b is null); assert(dtorCalled); assert(GC.addrOf(cast(void*) b) == null); // but be careful, a still points to it assert(a !is null); assert(GC.addrOf(cast(void*) a) == null); // but not a valid GC pointer
bool dtorCalled; interface A { int quack(); } class B : A { int a; int quack() { a++; return a; } ~this() { dtorCalled = true; } } A a = new B(); a.quack(); assert(GC.addrOf(cast(void*) a) != null); __delete(a); assert(a is null); assert(dtorCalled); assert(GC.addrOf(cast(void*) a) == null);
bool dtorCalled; struct A { string test; ~this() { dtorCalled = true; } } auto a = new A("foo"); assert(GC.addrOf(cast(void*) a) != null); __delete(a); assert(a is null); assert(dtorCalled); assert(GC.addrOf(cast(void*) a) == null);
int[] a = [1, 2, 3]; auto b = a; assert(GC.addrOf(b.ptr) != null); __delete(b); assert(b is null); assert(GC.addrOf(b.ptr) == null); // but be careful, a still points to it assert(a !is null); assert(GC.addrOf(a.ptr) == null); // but not a valid GC pointer
int dtorCalled; struct A { int a; ~this() { assert(dtorCalled == a); dtorCalled++; } } auto arr = [A(1), A(2), A(3)]; arr[0].a = 2; arr[1].a = 1; arr[2].a = 0; assert(GC.addrOf(arr.ptr) != null); __delete(arr); assert(dtorCalled == 3); assert(GC.addrOf(arr.ptr) == null);
© 1999–2019 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/core_memory.html