Allocation pressure occurs when a program creates objects faster or longer than garbage collection can reclaim them. Memory growth alone does not prove a leak: pressure can subside and memory return to baseline when load stops. section 11.2.2
Heavy writes to long-lived objects can make collection harder. In JavaScriptCore, write barriers may force the collector to revisit objects; its scheduler responds to allocation pressure by giving collection more CPU time, and can pause the application if its memory headroom runs out. 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);
}
Each request adds fresh objects to long-lived session objects and replaces an array, combining young-generation allocations with writes to old-generation data. section 9.3.4
To distinguish pressure from a leak:
- Run --trace-gc under load. Stable mu and a heap that returns to baseline when load stops indicate pressure; declining mu or a heap that stays elevated suggests a leak. section 11.2.2
- Reduce allocation rate or object lifetime to address pressure; find and remove the reference keeping objects alive to address a leak. section 11.2.2
Promise-heavy pipelines can add pressure too: each chunk in Web Streams incurs promise allocation, and wrapping synchronous work in an async transform adds further promises and microtasks. Keep synchronous CPU-bound work synchronous when the API allows it. section 10.4.1 section 10.7.1
