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
