Bird
0
0

In this code snippet, what causes the memory leak?

medium📝 Debug Q7 of 15
Node.js - Debugging and Profiling
In this code snippet, what causes the memory leak?
const cache = new Map();
function cacheData(key, value) {
  cache.set(key, value);
}
// cache never cleared
AFunction cacheData is recursive
BThe cache Map grows without removing old entries
CUsing Map instead of Object causes leaks
DMap keys are not strings
Step-by-Step Solution
Solution:
  1. Step 1: Analyze cache usage

    cache Map keeps growing as entries are added but never removed.
  2. Step 2: Understand memory impact

    Uncleared cache holds references, causing memory leaks.
  3. Final Answer:

    The cache Map grows without removing old entries -> Option B
  4. Quick Check:

    Uncleared cache causes leaks [OK]
Quick Trick: Clear cache entries when no longer needed [OK]
Common Mistakes:
  • Blaming Map type for leaks
  • Thinking function is recursive
  • Assuming keys type causes leaks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes