D Programming/Garbage collector

From Wikibooks, open books for an open world
Jump to: navigation, search

This page should be a documentation of the internals of the actual GC implementation, the interface, restrictions ...

Introduction[edit]

The actual Implementation has following characteristics:

  • conservative (vs. precise and internal pointers)
  • stop-the-world (vs. incremental vs. concurrent)
  • mark and sweep (vs. copying)
  • non-moving (vs. moving)

see also Garbage collection

File structure[edit]

std.thread
Support the GC with the actual position of the stackpointer.
/internal/gc/gc.d
the GC interface, a wrapper around the implementation.
/internal/gc/gcx.d
the GC implementation
/internal/gc/gcstub.d
stub for the gc, to prevent linking into application


Threads[edit]

To stop the world, the gc calls Thread.pauseAll(). The stop mechanism on Linux works with the Signals SIGUSR1 and SIGUSR2. In case of pausing the Thread, the signal handler stores the current stack pointer in stackTop, so this pointer is always uptodate.

How does it works[edit]

The GC is only called when memory should be allocated and currently none is available. If the gc is not able to free enough memory, new memory is requested from the system.

Entry point is gcx.d fullCollect()

  1. mark phase
    1. scan stacks of all threads
    2. scan roots (global variables)
    3. scan ranges (non gc managed memory which can also contain references to gc-objects)
  2. finalize all collected objects
  3. sweep the unused memory

Further information[edit]

Thoughts about better GC implementations