JS RuntimeBaseline · Widely

JavaScript Generational Garbage Collection

Mental Model

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.

Interactive Simulator

Orinoco Generational Scavenge & Mark-Sweep

Observe Young Generation semi-space evacuation and Old Generation promotion.

GC Roots
Young Gen (From)
Old Gen
Step 1 / 3: 1. Bump-Pointer Allocation

New short-lived objects allocate sequentially in Young Generation From-space.

const tmp = { x: 1, y: 2 };
Internal State / Memory
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.

  1. Root Enumeration: The collector starts from globals, active Call Stack frames, and DOM wrappers.
  2. Young Generation Scavenge: Objects allocate via bump-pointer in From-space; survivors copy to To-space, and dead objects cost zero to free.
  3. Promotion: Surviving two scavenges moves an object into the Old Generation.
  4. Concurrent Marking: Background worker threads trace Old Generation pointers alongside JS execution, coordinated with the Event Loop.
JS
// 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

Production Pitfall
  • Per-Frame Allocation Churn: Returning fresh { x, y } objects inside requestAnimationFrame saturates the Young Generation and triggers janky scavenges.
  • Finalizer Unreliability: FinalizationRegistry callbacks run only after an indeterminate GC cycle, never synchronously on scope exit.
When Not to Use
  • When not to use WeakRef: Never rely on WeakRef to manage critical resources like WebGL textures or file handles; call explicit .dispose() or using blocks instead.

Connections & Logical Next Steps

Seen in Architecture Autopsies