Tracing Collection with Mark and Sweep

Compute live objects as a reachability problem and keep the worklist invariant visible during tracing.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Roots seed reachability through references
  2. Sweep inspects objects the traversal never reached

Roots seed reachability through references

Roots can include live stack slots, registers and globals according to the runtime’s conventions. Mark each root object and scan its outgoing references, adding newly marked objects to a worklist. With a fixed graph during the pass, an invariant is that every marked object is reachable and every scanned object’s reachable immediate neighbors have been marked. Exhausting the worklist marks every object reachable by a finite path.

Sweep inspects objects the traversal never reached

A pair of objects referencing each other with no path from a root can be reclaimed. Direct reference counting cannot identify that case from counts alone. Sweeping the heap can involve work proportional to allocated areas even when few objects survive. An incremental tracer must additionally coordinate with mutations that could introduce references during marking. Generational collection traces selected regions using a record of incoming references.[1][2]

References

  1. Shriram Krishnamurthi, Programming Languages: Application and Interpretation ↩
  2. Robert Nystrom, Crafting Interpreters ↩