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
