liveBook · Manning

Death spiral

As explained in JavaScript in Depth

A death spiral is a garbage-collection failure mode in which repeated mutations force the collector to revisit objects faster than it can process them.

A death spiral occurs when heavy mutation of long-lived objects makes garbage collection revisit them faster than it can process them. The collector advances, a write barrier invalidates progress, and the collector returns to the changed object. If revisit requests keep arriving faster than they are serviced, collection cannot finish. section 9.3.4

JSC limits the failure mode through a space-time scheduler:

  • It reserves allocation headroom when collection begins, typically 50% above the triggering threshold. section 9.3.4
  • As the application consumes that headroom, the collector receives progressively more CPU time and the application receives less. section 9.3.4
  • If headroom is exhausted, JSC pauses the application until collection completes. section 9.3.4

A request-updated session store

An old-generation session object is mutated on every request with freshly allocated request data, copied headers, and a replacement history array. At high traffic, these writes trigger barriers while young-generation allocation continues, producing throttling and increased garbage-collection latency. 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);
}

Trace how a long-lived Map entry is repeatedly updated with short-lived objects and arrays. section 9.3.4

How JavaScript in Depth covers it

How deep it goes
Explains write-barrier revisits and scheduler throttling, but does not detail the collector’s complete implementation.
Examples
Uses an in-memory session store that updates long-lived sessions with newly allocated request objects and history arrays.
What it assumes
Assumes generational heaps, concurrent marking, write barriers, and allocation thresholds.
What you can do after
Diagnose/debug with it: recognize barrier-heavy mutation patterns and relate resulting latency to collector throttling.
How it connects
Connects concurrent marking, write barriers, generational garbage collection, incremental marking, and allocation pressure.

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.