Node.js Memory Management & Garbage Collection

Other Blogs

Blogs ❯❯ NodeJS

अगर आप 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 Memory Architecture : Stack vs Heap

Node.js V8 engine का use करता है — जो Google Chrome में भी use होता है। V8 memory को दो major parts में divide करता है -

1. Stack Memory

  • Stack में store होती हैं primitive values (numbers, strings, booleans, null, undefined).

  • Fast access के लिए use होती है। 

  • Limited in size (around 1MB–2MB per stack frame).

const a = 10; // Stack

2. Heap Memory

  • Heap में store होते हैं objects, arrays, functions — मतलब reference types .

  • यह large size का memory space होता है। 

  • Garbage collector heap memory को clean करता है।

const obj = { name: "Rabbit" }; // Heap

Garbage Collection (GC) in V8

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” हो सकता है।

⚠️ Common Causes of Memory Leaks

  • Global variables का overuse

  • Closures जो unnecessary data hold करते हैं। 

  • Event listeners जो remove नहीं किये गए। 

  • Large data in cache without expiration

  • Timers (setInterval) जो clear नहीं होते।

Tools to Debug Memory Leaks in Node.js

1. --inspect Flag with Chrome DevTools

node --inspect app.js
  • Chrome browser में open करो - chrome://inspect

  • Heap snapshot ले सकते हो। 

  • Memory timeline देख सकते हो।

2. heapdump Package

npm install heapdump

Add this in code

const heapdump = require('heapdump'); heapdump.writeSnapshot('./heap-' + Date.now() + '.heapsnapshot');

Snapshot को Chrome DevTools में open करके analyze करो।

3. Clinic.js (Zero-Config Profiler)

npm install -g clinic
clinic doctor -- node app.js
  • Automatically detect करेगा memory leaks, CPU spikes, etc.

  • Beautiful flamegraphs generate करेगा।

Real-World Example of a Memory Leak

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.

How to Avoid Memory Leaks?

  • Use weak references (e.g. WeakMap, WeakSet)

  • Avoid unbounded caches

  • Use removeListener when events are done

  • Clean up setInterval, setTimeout

  • Run memory profiling regularly

Conclusion

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 में हो सकते हैं।

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 5.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    Recent Blogs

    Loading ...

    0 Comment(s) found !