liveBook · Manning

Garbage collector churn

As explained in JavaScript in Depth

Garbage collector churn is a feedback loop in which allocation pressure causes increasingly frequent or expensive garbage-collection work that further delays application completion.

Garbage collector churn appears as a performance mystery: increasing allocation pressure makes collection more frequent or expensive, which delays requests and causes still more objects to remain alive. Young-generation collections pause the main thread; overlapping requests retain more objects, promote survivors, and trigger longer old-generation collections. section 9.3.4

The loop develops through a sequence of reinforcing effects:

  • Per-request allocation fills the young generation faster, increasing minor-GC frequency. section 9.3.4
  • Collection pauses lengthen requests, allowing new requests to overlap while earlier objects remain reachable. section 9.3.4
  • More surviving objects reach the old generation, where major collections add longer pauses and further backlog. section 9.3.4
  • V8 commonly enters churn through raw allocation volume; JavaScriptCore can enter a related death spiral when writes to old objects reference new objects. section 9.3.4
const sessions = new Map();  #A
function handleRequest(req) {
  let session = sessions.get(req.sessionId);
  if (!session) {
    session = { id: req.sessionId, created: Date.now() };
    sessions.set(req.sessionId, session);
  }
 
  // Every request mutates the long-lived session object
  // with fresh short-lived objects
  session.lastAccess = Date.now();
  session.lastRequest = {  #B
    url: req.url,
    headers: { ...req.headers },
    timestamp: Date.now(),
  };
  session.history = (session.history || [])
    .slice(-99)
    .concat([req.url]);  #C
 
  return buildResponse(req, session);
}

A request handler illustrates the pressure pattern: long-lived session objects are repeatedly mutated with fresh request data, copied headers, and history arrays. section 9.3.4

Diagnosing the runtime-specific pattern

V8 exposes collection type, pause duration, incremental marking steps, and mutator-utilization values through --trace-gc. JavaScriptCore and Bun do not provide an equivalent per-collection event trace here. section 9.3.4

How JavaScript in Depth covers it

How deep it goes
Traces young-generation fills, pauses, promotion, and major collections, then distinguishes V8 allocation churn from JSC mutation-driven failure.
Examples
Uses per-request session mutation, copied headers, history arrays, and V8 --trace-gc diagnostics.
What you can do after
Diagnose churn in V8 by correlating collection types, pauses, marking steps, and mutator-utilization values.
How it connects
Connects young generation, minor GC, old generation, major GC, write barriers, and incremental marking.

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.