liveBook · Manning

Scavenger

As explained in JavaScript in Depth

A young-generation garbage-collection process that copies surviving objects between semi-spaces and reclaims unreachable objects.

Why can a program create thousands of temporary objects without much garbage-collection cost, while retaining a small fraction of them causes pauses to grow? In V8, the Scavenger collects the young generation by copying objects that are still reachable. Dead objects are not individually freed; they are left behind when collection finishes. The crucial cost is often survival, not allocation. section 9.2.2 section 9.4.1

New objects enter the active semi-space, called from-space. When it fills, a minor GC begins. The Scavenger starts at roots and the remembered set, traces reachable young objects, and copies them into the other semi-space, to-space. After copying, it swaps the semi-space labels. The old from-space can be reused without visiting its dead objects; the copied survivors sit together without fragmentation. Work is therefore proportional to survivors rather than to every object allocated. section 9.2.2

Figure 9.4 During a scavenge, V8 traces live objects in from-space and copies them into to-space. Dead objects are never visited. Once copying is complete, the semi-space labels swap: the old from-space is abandoned and the old to-space becomes the new from-space for future allocations.

Follow the live objects across the two semi-spaces, then note that the labels swap while dead objects remain untouched. section 9.2.2

There is a catch: a young object can be reachable through an old-generation object, even though a minor GC does not scan the old generation. V8’s write barrier detects an old-to-young reference stored in a property and records it in the remembered set. The Scavenger treats those recorded references as additional tracing roots. That preserves reachable objects, but adds bookkeeping to property writes on long-lived objects. Objects that keep surviving scavenges can eventually be promoted, shifting collection work toward the old generation. section 9.2.4 section 11.2.5

Same allocation rate, different lifetime

Imagine a request creating 5,000 response objects. If it returns only a count, those objects can die young. If it stores a slice of the response in a persistent cache on every request, some remain reachable. The two versions create the same objects per request, yet retaining them changes the GC profile: Scavenges alone can give way to growing old-generation collections and longer pauses. An intermediate array from a filter/map chain, by contrast, may die young and be handled efficiently; replacing every chain with a loop is not automatically an improvement. section 9.2.4 section 9.4.1

When Scavenges appear in a profile, distinguish frequency from survival:

  • Frequent effective Scavenges can mean high allocation volume: the young generation fills quickly even though most objects die young. section 11.2.5
  • Regular Mark-Compact events under load suggest objects are surviving into the old generation. Examine retained objects rather than assuming the number of allocations is the problem. section 11.1.5 section 11.2.5
  • Keep an eye on closures captured by promises, timers, and event listeners; they can extend object lifetimes. Profile under realistic load before rewriting readable code. section 9.4.2

This is a V8 collector, not a JavaScript-language rule or a description of every runtime. On Node.js, `--trace-gc` labels young-generation collections `Scavenge` and reports heap sizes and pause durations. Browser DevTools exposes GC events in a timeline instead of the same text log; JSC-based runtimes do not provide V8’s continuous `--trace-gc` view. section 11.1.5

How JavaScript in Depth covers it

How deep it goes
Works through semi-space copying, label swaps, remembered-set roots, promotion, and trace interpretation without specifying a fixed promotion threshold.
Examples
Contrasts cached request results with short-lived results and compares intermediate arrays against single-pass loops.
What it assumes
Builds the mechanism from roots and two semi-spaces while assuming familiarity with JavaScript objects, properties, and references.
What you can do after
Diagnose/debug with Node.js `--trace-gc` by distinguishing frequent effective Scavenges from old-generation collections caused by survivors.
How it connects
Connects to generational garbage collection, write barriers, remembered sets, object promotion, Mark-Compact, and heap profiling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.