Garbage Collector: The Memory Cleanup Game

You are the garbage collector. Objects hang off the root by arrows, and as the program runs, references are dropped. Anything you can no longer reach from the root by following the arrows is garbage: tap it to free the memory. Tap a live object and the program crashes. Let garbage pile up and you run out of memory.

Freed0 Combox1 Best0 ♥♥♥
Memory0/20

Tap or click an object to free it. A red dashed line marks a reference that was just dropped.

Can you keep the heap clean?

Follow the arrows from root. Objects with no path from root are garbage: free them. Three wrong taps or a full memory ends the run.

How to play

  1. Press Start game. The dark square is the root; circles are objects, and an arrow from A to B means A holds a reference to B.
  2. The program keeps creating objects and dropping references. When a reference disappears it flashes as a red dashed line.
  3. An object is live if you can walk from root to it along the arrows. Otherwise it is garbage, even if other garbage still points at it.
  4. Tap garbage to free it. Freeing several objects quickly builds a combo.
  5. Tapping a live object costs a life (a crash). The game ends after three crashes, or when memory is full and the program cannot allocate a new object.

Scoring

ActionEffect
Free a garbage object+1 freed; points × combo
Free another within 2 secondsCombo +1, up to ×5
Tap a live objectCrash: −1 life, combo resets
Memory reaches 20 objectsOut of memory: game over

How real garbage collectors decide

The game follows the rule used by the garbage collectors in JavaScript engines, Java, Go and .NET: reachability. The engine starts from a set of roots, such as global variables and the local variables of running functions, marks everything it can reach through references, and then frees the rest. This is called mark-and-sweep (MDN: Memory management).

Watch for cycles. Two objects that point at each other but have no path from root are still garbage. An older technique, reference counting, could not free them because each object still had one reference; mark-and-sweep can. In the game, a pair of circles with arrows between them can be unreachable and safe to free.

In your own code, an object stays in memory as long as something reachable refers to it: a forgotten event listener, a timer, a global cache or a closure can keep a large object alive. That is what a memory leak in JavaScript usually is.

Tips

Frequently asked questions

What is a garbage collector in programming?

It is the part of a language runtime that frees memory automatically. Instead of the programmer releasing every object, the collector periodically finds objects that can no longer be reached and reclaims their memory.

Can garbage become live again?

No. Once nothing reachable refers to an object, the program has no way to get a reference back to it, which is why the collector can free it safely. The game follows the same rule.

Does it work on a phone?

Yes. Tap the objects directly; the touch area is larger than the circle.

Where is my best score saved?

Only in this browser’s local storage. Nothing is sent anywhere.

Related tools