Diagnosing and Fixing Node.js Memory Leaks | NodeFlux.eu Docs
Identify uncollected object references, event listener leaks, and global state accumulation in JavaScript.
Steps
- Monitor memory graph trends in your service manager view over 24-48 hours.
- Identify memory leaks: RAM usage steadily rises linearly without dropping after garbage collection cycles.
- Check common leak culprits: global arrays/objects used as caches without size limits or TTL expiration.
- Verify event listeners: ensure emitter.removeListener() or once() is called for temporary event subscriptions.
- Check unclosed database pools or un-ended HTTP response streams.
- Use process.memoryUsage() logging in development to track heapUsed vs heapTotal metrics.
Notes
- V8 engine default memory limit can be explicitly tuned using --max-old-space-size=400 flag.
- LRU (Least Recently Used) cache libraries like lru-cache prevent infinite map growth.
Related NodeFlux pages
- Managing Node.js Process Lifecycles with PM2 — Use PM2 process manager configuration files to handle auto-restarts, environment injection, and log management.
- Setting Up CORS and Web Application Security Headers — Configure Cross-Origin Resource Sharing (CORS) and Helmet security headers on Node.js web services.
- NPM Dependency Management: package.json vs package-lock.json — Avoid production deployment crashes by managing NPM dependency trees cleanly with lockfiles.