Yashotantra Academy logo
← Node.js theorytheory 0/50 · 0%
level 1
Curious · 0 XP
streak
0d
perfect
0
badges
0

100 XP to Apprentice

Performance · hard

39. Memory management and leaks

How V8's garbage collector works and common leak patterns.

1 min read3 questionsup to 50 XPtopic 39/50

Read in another language

AI translates the explanation and quiz text while preserving code and answer scoring.

V8 automatically reclaims memory no longer reachable from any root reference (globals, the call stack, closures). A memory leak in JavaScript means objects that should be garbage are still reachable — commonly through a growing array/Map that's never cleared, forgotten event listeners, or closures capturing more than intended.

js
// leak: this array grows forever, and the listener is never removed
const cache = [];
emitter.on("data", (chunk) => cache.push(chunk));

// fix: bound growth and remove listeners when done
const cache2 = [];
const MAX = 1000;
function onData(chunk) {
  cache2.push(chunk);
  if (cache2.length > MAX) cache2.shift();
}
emitter.on("data", onData);
// later: emitter.off("data", onData);

`node --inspect` plus Chrome DevTools' heap snapshot tool is the standard way to diagnose a real leak: take two snapshots under load, minutes apart, and compare which object types grew unexpectedly.

How to study this topic

  1. 1. Read the explanation once end to end, skipping the code.
  2. 2. Re-read the code snippets and say out loud what each line does.
  3. 3. Answer the quiz without scrolling back — that is the real check.
  4. 4. Read every explanation, even for the answers you got right.

Scoring

  • · Up to 30 XP from quiz accuracy.
  • · +15 XP for a perfect first attempt.
  • · +5 XP while a 3-day streak is running.
  • · 75% or higher marks the topic complete.

Check your understanding

  1. 1. What does a memory leak in JavaScript actually mean?

  2. 2. What is a common cause of leaks in long-running Node servers?

  3. 3. What tool helps diagnose a real memory leak?

Badges

First light
Pass your first topic
Sharp shooter
3 perfect first-try quizzes
On a roll
Study 3 days in a row
Week warrior
Study 7 days in a row
Grinder
Earn 500 XP
Deep diver
Earn 1500 XP