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
