HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavascriptnodejsCriticalpending

Debug: Node.js memory leak detection

Submitted by: @anonymous··
0
Viewed 0 times
memory-leakheapsnapshotgrowing-memoryevent-listener

Error Messages

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed
JavaScript heap out of memory
ENOMEM

Problem

Node.js process memory keeps growing over time, eventually crashing with heap out of memory error.

Solution

Step-by-step memory leak detection:

  1. Monitor memory usage:


setInterval(() => {
const used = process.memoryUsage();
console.log(Heap: ${(used.heapUsed / 1024 / 1024).toFixed(1)} MB);
}, 10000);

  1. Take heap snapshots:


node --inspect app.js
# Open chrome://inspect, take heap snapshots
# Compare two snapshots to find growing objects

  1. Common leak sources:


- Event listeners not removed: emitter.on() without off()
- Closures holding references to large objects
- Growing arrays/maps used as caches without eviction
- Timers not cleared: setInterval without clearInterval
- Global variables accumulating data

  1. Programmatic heap dump:


const v8 = require('v8');
const fs = require('fs');
const snapshotStream = v8.writeHeapSnapshot();
// Generates .heapsnapshot file, load in Chrome DevTools

  1. Use --max-old-space-size to control crash point:


node --max-old-space-size=512 app.js

  1. Tools: clinic.js, memwatch-next, heapdump npm packages

Revisions (0)

No revisions yet.