JavaScript Generational Garbage Collection
V8's Orinoco collector splits the heap into a fast Young Generation (semi-space Scavenger for short-lived allocations) and an Old Generation (concurrent tri-color Mark-Sweep-Compact). Reachability from the Call Stack and globals (not reference counting) determines what survives.
Orinoco Generational Scavenge & Mark-Sweep
Observe Young Generation semi-space evacuation and Old Generation promotion.
New short-lived objects allocate sequentially in Young Generation From-space.
const tmp = { x: 1, y: 2 };- fromSpace
- [ "tmp", "req", "btnRef" ]
- toSpace
- []
- oldGen
- [ "appStore" ]
Under the Hood
V8's Orinoco collector partitions the heap into a Young Generation (two semi-spaces scavenged via Cheney's copying algorithm) and an Old Generation (concurrent tri-color Mark-Sweep-Compact). Short-lived allocations evacuate from From-space to To-space in microseconds, while background threads trace Old Generation pointers alongside JS execution.
- Root Enumeration: The collector starts from globals, active Call Stack frames, and DOM wrappers.
- Young Generation Scavenge: Objects allocate via bump-pointer in From-space; survivors copy to To-space, and dead objects cost zero to free.
- Promotion: Surviving two scavenges moves an object into the Old Generation.
- Concurrent Marking: Background worker threads trace Old Generation pointers alongside JS execution, coordinated with the Event Loop.
// Reuse buffers instead of allocating per animation frame
const scratch = new Float32Array(4);
function projectPoint(x, y) {
scratch[0] = x * 1.5;
scratch[1] = y * 1.5;
return scratch;
}Problem Genealogy
- 1999 (Stop-the-World Mark-Sweep): Single-threaded pauses froze pages for hundreds of milliseconds on large heaps.
- 2017 (V8 Orinoco): Parallel scavenging and concurrent marking cut main-thread GC pauses below a few milliseconds.
- 2021 (ES2021
WeakRef&FinalizationRegistry): Exposed non-deterministic weak references for external caches.
Production Pitfalls
- Per-Frame Allocation Churn: Returning fresh
{ x, y }objects insiderequestAnimationFramesaturates the Young Generation and triggers janky scavenges. - Finalizer Unreliability:
FinalizationRegistrycallbacks run only after an indeterminate GC cycle, never synchronously on scope exit.
- When not to use
WeakRef: Never rely onWeakRefto manage critical resources like WebGL textures or file handles; call explicit.dispose()orusingblocks instead.
Connections & Logical Next Steps
Garbage collection reclaims only unreachable subgraphs; any remaining retainer path pins the entire cluster.
Pre-allocated TypedArray views eliminate per-element heap boxing and Young Generation GC churn.
String marshaling and Wasm memory.grow() detaching views trigger JS heap allocations and GC pressure.
Active Call Stack frames act as primary GC roots that keep local bindings reachable.
V8 schedules incremental marking steps and idle-time GC work between Event Loop tasks.
Transferring an ArrayBuffer detaches it in the source realm so GC reclaims only the empty shell.