No background needed · 9 lessons · Free

Concurrent Garbage Collector & Tri-Color Write-Barrier Lab

See how JavaScript, Go, Java, and C# clean up unused memory in tiny per-frame slices (60 frames per second) without freezing your app—and how arrow-write guards protect live boxes. Cleaning up in slices is what makes a collector concurrent, and the guard is its write barrier.

5-Step Interactive Studio

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
Predict: which boxes get freed?

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
Predict: what happens when the wave reaches the loop?

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
Predict: with your settings, does any frame stutter?

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
Predict: after the move, is W kept?

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
Predict: which box still in use gets freed?