Node.js --expose-gc for Memory Leak Debugging
Problem
Long-running Node.js apps (MQTT clients, batch processors) show increasing memory usage. Hard to tell if GC is working properly or if there’s a real leak.
Solution
# --expose-gc enables global.gc()
node --expose-gc app.js
# In Docker
CMD ["node", "--expose-gc", "--max-old-space-size=512", "dist/index.js"]
function logMemory(label) {
if (global.gc) global.gc(); // force GC
const usage = process.memoryUsage();
console.log(`[${label}] Heap: ${Math.round(usage.heapUsed / 1024 / 1024)}MB / ${Math.round(usage.heapTotal / 1024 / 1024)}MB`);
}
logMemory('start');
await processLargeData();
logMemory('after'); // if heap doesn't shrink after GC → leak
Key Points
- If
heapUsedkeeps growing afterglobal.gc(), it’s a real memory leak — objects are referenced somewhere GC can’t reach. --max-old-space-size=512limits heap size so leaks trigger OOM errors faster, making them easier to catch.- Don’t use
--expose-gcin production. Manual GC impacts performance. Use it only for debugging.