Project 2: The “Stop-The-World” Mark-and-Sweep

A tiny virtual machine with a fixed heap. When the heap is full, the VM “stops,” scans all global and stack variables, marks everything reachable, and sweeps (frees) the rest.

Quick Reference

Attribute Value
Primary Language C++
Alternative Languages Go, Python
Difficulty Level 3: Advanced
Time Estimate See main guide
Knowledge Area Data Structures / Graph Traversal
Tooling Custom VM/Heap allocator
Prerequisites See main guide

What You Will Build

A tiny virtual machine with a fixed heap. When the heap is full, the VM “stops,” scans all global and stack variables, marks everything reachable, and sweeps (frees) the rest.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • Root Scanning → How do you find the “start” of the graph?
  • DFS vs BFS for marking → maps to Stack overflow risks during GC
  • Bitmaps vs. Object Headers → Where do you store the “Marked” bit?

Key Concepts

  • Map the project to core concepts before you code.

Real-World Outcome

Allocation failed! Starting GC...
[GC] Roots: 4
[GC] Marked: 152 objects
[GC] Swept: 45 objects (12 KB freed)
Allocation resumed.

Implementation Guide

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. Refactor into clean modules with tests.

Milestones

  • Milestone 1: Minimal working program that runs end-to-end.
  • Milestone 2: Correct outputs for typical inputs.
  • Milestone 3: Robust handling of edge cases.
  • Milestone 4: Clean structure and documented usage.

Validation Checklist

  • Output matches the real-world outcome example
  • Handles invalid inputs safely
  • Provides clear errors and exit codes
  • Repeatable results across runs

References

  • Main guide: LEARN_MODERN_GC_DEEP_DIVE.md
  • “The Garbage Collection Handbook” by Richard Jones