liveBook · Manning

Allocation pressure

As explained in JavaScript in Depth

A condition in which memory usage rises because a program is allocating objects faster or longer than reclamation can offset.

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

How JavaScript in Depth covers it

How deep it goes
Explains write-barrier revisits, scheduler throttling, and promise allocations, then gives a heap-based diagnostic; it does not detail collector internals.
Examples
Uses a request-updated session cache, a promise-heavy processor under load, and per-chunk stream promises as examples.
What you can do after
Diagnose memory growth with --trace-gc, then reduce allocation rate or object lifetime when load-related pressure is indicated.
How it connects
Connects garbage collection and memory leaks with write barriers, promises, and Web Streams backpressure.

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.