Free to Learn · Garbage collector lab
Catch a collector freeing the wrong box
A program keeps data in memory boxes. A box can hold an arrow to another box. Roots are the boxes the program uses directly. A collector tries to free boxes the program can no longer reach. Guess each result, then run it.
1. Abandoned Loops
Pairs with Part 1
The program holds A directly. A and B point at each other. A counting collector frees a box when its incoming arrow count hits zero. What happens when the program lets go of A?
drop the program’s arrow to A
free boxes with zero arrows in
free boxes no root can reach
2. Three-Color Wave
Pairs with Part 2
Tracing paints boxes. Every box starts white: not found yet. A box a root points at turns grey: found, arrows not checked. Checking a grey box paints each white box it points at grey, then the box turns black: finished. Here A, B and C form a loop, and D is alone.
while a grey box remains:
take a grey box
follow each of its arrows:
if the target is white:
paint the target grey
paint the first box black
3. Slice Per Frame
Pairs with Part 3
A game draws one picture, a frame, every 16 milliseconds. Each frame leaves spare time to check 300 boxes. This heap, all the program's boxes, holds 2,000. Try changing how many boxes the collector checks before it lets the game draw again.
while a grey box remains:
check one box
if this slice is full:
wait for the next frame
4. Hidden Arrow Swap
Pairs with Part 4
Between slices, the program can move an arrow. It holds X and G directly. The collector has finished X and will check G next. G holds the only arrow to W, and Z is alone. The program moves that arrow from G to X. Try changing when the move happens.
pause after checking X
move G’s arrow to X now
finish checking grey boxes
move G’s arrow to X now
free the remaining white boxes
5. Arrow Write Guard
Pairs with Part 5
The fix is a guard that runs whenever the program stores an arrow. If a black box gets an arrow to a white box, the guard paints the white box grey and adds it to the to-visit list: the grey boxes still waiting to be checked. Same swap as step 4, except W also points at box C.
X gets an arrow to W
if X is black and W is white:
paint W grey
add W to the to-visit list
erase the arrow from G to W
Three Ways In · Layer 3 of 3 · Node.js 22+ · Zero Dependencies
Build With Your Coding Agent — 5 Rungs + Planted Traps
Build an incremental, concurrent, generational garbage collector with your coding agent across five rungs: cycle-safe root tracing, explicit-worklist tri-color mark-sweep, bounded per-frame incremental slices, Dijkstra/Yuasa write barriers, and nursery card tables. Each rung gives you an architect prompt for Claude Code, Codex, or Cursor, a planted bug that unguided agents write, and a deterministic test suite that catches it.
- Reference counts & cycles: Immediate refcount reclamation paired with backup tracing for cycles — Planted trap: Decrementing cyclic subgraph counts optimistically and freeing still-reachable objects.
- Tri-color mark-sweep: White/grey/black worklist marker and sweep phase with zero recursion overflow — Planted trap: Marking nodes black before scanning children or failing to reset survivors to white on sweep.
- Incremental frame slices: Work-budgeted collector stepping k objects per frame beside a live mutator — Planted trap: Allocating brand-new objects white during an active sweep so freshly created objects get freed.
- Write barriers: Dijkstra insertion and Yuasa snapshot-at-the-beginning barriers — Planted trap: Guarding black→white writes only when the slot was empty, or dropping overwritten grey edges.
- Generational nursery & cards: Fast bump-pointer nursery collection using an old-to-young card table — Planted trap: Clearing or skipping card-table marks when a survivor stays pointing at another young object.
unzip agent-track.zip && cd gc-tricolor-lab/agent-track
node --test test/*.test.mjs
mkdir -p work
IMPL=work node --test test/rung-01.test.mjs
Download 5-Rung Agent Track (agent-track.zip) Read AGENT-TRACK.md on GitHub Open agent-track/README.md on GitHub
Run the collector tests on your computer
git clone https://github.com/itsHabib/free-to-learn.git
cd free-to-learn
git switch --detach 7dba0d743224c10a707307769782b0db3f9a2a1a
cd courses/gc-tricolor-lab
node --test tests/gc-tricolor.test.mjs
All Free to Learn courses · Give feedback on this course · Usage & privacy