Stop Node.js Memory Leaks: A Practical Step-by-Step Guide to Diagnose, Fix, and Prevent Them
Intro — why this matters: Memory leaks in Node.js services are stealthy. They slowly increase resource usage, cause GC storms and latency spikes, trigger OOMKilled events in Kubernetes, and ultimately lead to outages and engineering firefights. This guide gives a production-safe, end-to-end playbook to detect a nodejs memory leak, capture heap snapshot nodejs evidence, analyze retained paths, fix common patterns, and add CI and observability to prevent regressions. TL;DR Checklist Quick triage: compare RSS vs heapUsed , inspect GC logs ( --trace-gc ), and check OOMKilled events. Low-impact probes in prod: small heapdump via heapdump on signal or sampling profiler (Clinic) — avoid long blocking operations. Reproduce locally: traffic replay, synthetic load, same Node flags ( --max-old-space-size ). Capture artifacts: heap snapshots, CPU profiles, flamegraphs, and trace-gc output. Analyze: find dominating objects, retained paths, and closure leaks. Fix with concrete ...