Skip to content

FlashcardNot attempted

Garbage collection

Recall it before you reveal

How does JavaScript decide what memory to free?

Space to reveal
Show answer & explanation

Reachability, via mark-and-sweep: starting from the roots (globals and the current call stack), the collector marks everything reachable by following references; anything left unmarked is swept.

So "unused" isn't enough: an object is only freed when nothing reachable references it. Modern engines are generational (most objects die young) and incremental, to keep pauses short.