If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
by Juanjo Jaramillo on Unsplash
अगर आप mid या senior-level Node.js developer हो — या किसी backend system पर काम कर रहे हो जहाँ service long-running होती है — तो आपको memory management का solid understanding होना जरूरी है।
Node.js memory leak एक common issue है production apps में, और अगर आप heap, stack, GC (Garbage Collector) को नहीं समझते, तो आपका app slow या crash कर सकता है।
इस blog में हम cover करेंगे -
Node.js & V8 का memory architecture
Heap vs Stack
Garbage Collection कैसे काम करता है
Memory leak कैसे detect करें (with tools)
●●●
Node.js V8 engine का use करता है — जो Google Chrome में भी use होता है। V8 memory को दो major parts में divide करता है -
Stack में store होती हैं primitive values (numbers, strings, booleans, null, undefined).
Fast access के लिए use होती है।
Limited in size (around 1MB–2MB per stack frame).
const a = 10; // StackHeap में store होते हैं objects, arrays, functions — मतलब reference types .
यह large size का memory space होता है।
Garbage collector heap memory को clean करता है।
const obj = { name: "Rabbit" }; // Heap●●●
Garbage Collection का मतलब: unused memory को free कर देना।
कैसे काम करता है GC?
V8 track करता है किस variable का reference exist करता है।
जो reference accessible नहीं होता, वो "garbage" ban जाता है।
GC उस memory को automatically clean कर देता है।
But GC हर वक्त run नहीं होता। अगर memory leak हो गया, तो GC उसे detect नहीं करेगा — क्योंकि object technically “reachable” हो सकता है।
Global variables का overuse
Closures जो unnecessary data hold करते हैं।
Event listeners जो remove नहीं किये गए।
Large data in cache without expiration
Timers (setInterval) जो clear नहीं होते।
●●●
node --inspect app.js
Chrome browser में open करो - chrome://inspect
Heap snapshot ले सकते हो।
Memory timeline देख सकते हो।
npm install heapdump
Add this in code
const heapdump = require('heapdump');
heapdump.writeSnapshot('./heap-' + Date.now() + '.heapsnapshot');Snapshot को Chrome DevTools में open करके analyze करो।
npm install -g clinic clinic doctor -- node app.js
Automatically detect करेगा memory leaks, CPU spikes, etc.
Beautiful flamegraphs generate करेगा।
●●●
const cache = {};
app.get('/store', (req, res) => {
const bigData = Buffer.alloc(1e6); // 1 MB
cache[Date.now()] = bigData;
res.send('Data stored!');
});हर request पर new data add हो रहा है, लेकिन कभी delete नहीं हो रहा। यह memory gradually fill कर देगा , means Memory leak.
●●●
Use weak references (e.g. WeakMap, WeakSet)
Avoid unbounded caches
Use removeListener when events are done
Clean up setInterval, setTimeout
Run memory profiling regularly
Node.js की performance तभी sustain कर सकती है जब आप उसकी memory lifecycle समझो, अगर आपका app long-running, high-traffic या real-time है, तो -
Memory profiling एक regular process होना चाहिए।
Tools का use करो, random crashes को avoid करो।
Garbage collector पर blindly depend mat रहो — leaks आपके logic में हो सकते हैं।
●●●
Loading ...