Garbage Collector Garden — Grow, Trace, and Compact a Living Heap
Plant objects in a finite heap, wire their references, sever their roots, and watch tracing collection prove that reachability—not cycles—decides what survives.
What this is
Garbage Collector Garden is a local, deterministic heap laboratory. Plant variable-sized objects, connect their fields, pin roots, and then advance a tracing collector one atomic action at a time or let it run. The bundled cycle/leak lab contains both a rooted cache cycle that survives and an isolated ivy cycle that must disappear, making the difference between circularity and reachability impossible to miss.
Why this is mind-blowing
The abstract mechanics become physical: gray frontier objects turn into traced survivors, white garbage is swept into visible holes, compacting slides live blocks toward address zero, and stale pointer words glow until each one is rewritten. The same heap can be projected through mark–sweep and copy-like compaction while live bytes, reclaimed bytes, fragmentation, pause cost, and invariants keep every visual claim honest.
Prompt
Build “Garbage Collector Garden — Grow, Trace, and Compact a Living Heap,” a beautiful, accurate single-file interactive heap visualizer. Users allocate objects, add/remove references, pin/unpin roots, create a cycle/leak preset, and run mark, sweep, and compact phases step-by-step or continuously. Include at least mark-sweep and a copying/compacting comparison, clearly distinguish reachable/unreachable objects, animate relocation and pointer rewrites, expose fragmentation, pause time, live bytes, reclaimed bytes, and invariant checks. Cycles with no root must be collected. Add deterministic presets/seed, reset/play/pause/step, keyboard and pointer/touch usability, accessibility, responsive 390px layout, and reduced motion. Self-contained inline HTML/CSS/JS only; no external resources/network/build; <=160KB.
Paste into your favorite AI and adapt it to your context.
What I learned shipping it
- Tracing collectors care about paths from roots, not reference counts: a rootless cycle is ordinary garbage, while a rooted cycle can retain an entire subgraph.
- Mark–sweep buys stable addresses at the cost of holes; relocation buys one large free region but requires forwarding metadata and a rewrite for every moved pointer.
- A collector visualization becomes trustworthy only when survivor preservation, pointer validity, non-overlap, and post-collection density are checked as executable invariants.