How to Debug Memory Leaks in Node.js Production Apps
How to Debug Memory Leaks in Node.js Production Apps Your Node.js app works fine for hours, then slows to a crawl. Memory climbs steadily. A restart fixes it temporarily — then it happens again. Th...

Source: DEV Community
How to Debug Memory Leaks in Node.js Production Apps Your Node.js app works fine for hours, then slows to a crawl. Memory climbs steadily. A restart fixes it temporarily — then it happens again. That's a memory leak. Here's how to find and fix it. Confirm It's a Leak # Watch RSS memory of your Node process over time watch -n 30 'ps -o pid,rss,vsz,comm -p $(pgrep -f "node server")' # Or log it from inside the app setInterval(() => { const m = process.memoryUsage(); console.log(JSON.stringify({ rss: Math.round(m.rss / 1024 / 1024) + 'MB', heap: Math.round(m.heapUsed / 1024 / 1024) + 'MB', time: new Date().toISOString() })); }, 60000); If RSS grows steadily and never drops, you have a leak. Step 1: Generate a Heap Snapshot # Start Node with inspector node --inspect server.js # Or send signal to running process kill -USR1 <PID> # Opens inspector on port 9229 Then in Chrome: chrome://inspect → Open dedicated DevTools → Memory → Take heap snapshot. Take a snapshot, do some actions,