liveBook · Manning

Garbage collection pressure

As explained in JavaScript in Depth

The memory-management pressure caused by allocation or object retention that makes garbage collection run frequently or expensively.

A heap graph that climbs, holds, then drops can indicate garbage collection pressure rather than a memory leak. The collector is reclaiming memory correctly, but allocation or object survival makes it work too hard. section 11.0 section 11.6

Pressure has two mechanisms. High allocation volume fills the young generation quickly and triggers frequent Scavenges. Longer-lived objects survive copying between semi-spaces, enter the old generation, and increase Mark-Compact work. section 11.2.5

Common sources expose which mechanism is active:

  • Volume-driven pressure: string concatenation in loops, array method chains, repeated JSON.parse calls, and template interpolation create many short-lived objects. section 11.2.5
  • Lifetime-driven pressure: concurrent promises, timer or event-listener closures, and cross-request caches retain objects long enough for promotion. section 11.2.5
  • Constrained deployment environments amplify either form when the workload has insufficient memory headroom. section 11.2.6

Diagnosing a delay spike

Check event-loop delay, then correlate elevated delay with --trace-gc. Frequent Scavenges suggest allocation volume; Mark-Compact events under load suggest promotion. If GC events align with delay spikes, investigate pressure; otherwise use CPU profiling to find synchronous work. section 11.3.1 section 11.6

Reduce pressure by matching the fix to the mechanism:

  • Use allocation sampling to locate high-volume sites; replace repeated intermediate allocations with array join, cached parsing, or single-pass loops where appropriate. section 11.2.6
  • Compare snapshots to identify survivors, then shorten the lifetimes of retained objects when possible. section 11.2.6

How JavaScript in Depth covers it

How deep it goes
Explains young- and old-generation mechanisms, diagnostic signals, causes, and targeted reductions, but defers engine-specific internals.
Examples
Uses heap graphs, trace-gc output, allocation timelines, snapshot comparisons, string operations, array chains, parsing, promises, closures, and caches.
What it assumes
Assumes familiarity with JavaScript memory measurement, generational collection, event-loop delay, and basic runtime diagnostics.
What you can do after
Diagnose/debug with trace-gc, allocation sampling, heap snapshots, event-loop delay, and CPU profiles, then target volume or lifetime.
How it connects
Connects garbage collection pressure to memory leaks, heap snapshots, allocation sampling, event loop delay, and CPU profiling.

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.