liveBook · Manning

Space-time scheduler

As explained in JavaScript in Depth

A space-time scheduler is a runtime scheduler that trades application execution time for garbage-collector CPU time as memory headroom is consumed.

A garbage collector can enter a death spiral when a program repeatedly writes to old objects faster than the collector can revisit them. A space-time scheduler prevents this by trading application execution time for collector CPU time as memory headroom is consumed. section 9.3.4

At collection start, JSC reserves headroom, typically 50% above the triggering allocation threshold. As allocation consumes it, the collector receives progressively more CPU while the application receives less; exhausting it pauses the application until collection finishes. section 9.3.4

Interpret the resulting behavior as a workload-dependent tradeoff:

  • Eden collections often remain under a millisecond, but aggressive allocation combined with writes to old objects can produce increased garbage-collection latency. section 9.3.4
  • An in-memory session store can trigger the problem when long-lived sessions receive freshly allocated request objects on every request. 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 a session cache whose long-lived entries receive new request, header, and history objects on every call. section 9.3.4

How JavaScript in Depth covers it

How deep it goes
Explains headroom allocation, progressive CPU redistribution, full exhaustion behavior, and workload-specific latency.
Examples
Uses an in-memory session store that mutates persistent sessions with freshly allocated request data.
What it assumes
Assumes familiarity with garbage collection, old and young generations, allocation thresholds, and write barriers.
What you can do after
Diagnose/debug garbage-collection latency caused by heavy allocation alongside repeated writes to old objects.
How it connects
Connects garbage collection with write barriers, allocation pressure, incremental marking, and generational memory.

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.