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
