Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a memory leak in Node.js?
A memory leak happens when a program keeps using more and more memory because it doesn't release memory it no longer needs. This can slow down or crash the app.
Click to reveal answer
beginner
How can global variables cause memory leaks?
Global variables stay in memory for the whole app life. If you store large data there and never clear it, memory keeps growing.
Click to reveal answer
intermediate
Why do event listeners cause memory leaks?
If you add event listeners but never remove them, they keep references to objects, so memory can't be freed.
Click to reveal answer
intermediate
What is a closure-related memory leak?
Closures keep references to variables even after the outer function finishes. If those variables hold big data and are not cleared, memory leaks happen.
Click to reveal answer
beginner
How can caching cause memory leaks?
If you cache data without limits or expiration, the cache grows forever, using more memory.
Click to reveal answer
Which of these is a common cause of memory leaks in Node.js?
ANot removing event listeners
BUsing async/await
CWriting synchronous code
DUsing console.log
✗ Incorrect
Event listeners keep references to objects. If not removed, they cause memory leaks.
What happens if you keep large objects in global variables?
AThey get garbage collected immediately
BThey cause syntax errors
CThey stay in memory for the app lifetime
DThey improve performance
✗ Incorrect
Global variables stay in memory as long as the app runs, causing memory to grow if large objects are stored.
How can closures lead to memory leaks?
ABy freeing memory too early
BBy keeping references to variables longer than needed
CBy causing syntax errors
DBy blocking event loops
✗ Incorrect
Closures keep variables alive, so if those variables hold big data, memory is not freed.
What is a safe way to avoid cache-related memory leaks?
ASet limits or expiration on cache
BUse unlimited cache size
CNever use cache
DStore cache in global variables
✗ Incorrect
Setting limits or expiration ensures cache does not grow forever.
The correct method to remove a specific listener is removeListener(event, callback).
Step 2: Check each option's validity
emitter.removeListener('event', callback); uses the correct method. emitter.off('event'); removes all listeners but omits the specific callback. Options A and C are not valid methods.
Final Answer:
emitter.removeListener('event', callback); -> Option D
What will be the output and what memory issue might this cause if keys are never removed?
medium
A. 0; No memory leak because cache is empty
B. 2; Memory leak due to unbounded cache growth
C. Error; Syntax error in code
D. Undefined; cache is not defined
Solution
Step 1: Analyze the code output
The cache object stores two keys: 'user1' and 'user2'. Object.keys(cache).length returns 2.
Step 2: Identify memory issue
Since keys are never removed, cache grows indefinitely, causing a memory leak.
Final Answer:
2; Memory leak due to unbounded cache growth -> Option B
Quick Check:
Cache size = 2, leak if keys never removed [OK]
Hint: Uncleared caches cause leaks; count keys to check size [OK]
Common Mistakes:
Thinking cache is empty initially
Assuming syntax error without checking code
Confusing undefined with empty object
4. You have this code causing a memory leak:
const listeners = [];
function addListener(emitter, callback) {
emitter.on('data', callback);
listeners.push({emitter, callback});
}
// Later you forget to remove listeners
What is the best fix to prevent the leak?
medium
A. Remove listeners with emitter.removeListener and clear the listeners array
B. Do nothing; Node.js cleans up automatically
C. Replace emitter.on with emitter.once
D. Use global variables instead of arrays
Solution
Step 1: Identify cause of leak
Listeners are added and stored in an array but never removed, causing memory to stay used.
Step 2: Apply fix to remove listeners and clear references
Use emitter.removeListener with stored callbacks and clear the listeners array to free memory.
Final Answer:
Remove listeners with emitter.removeListener and clear the listeners array -> Option A
Quick Check:
Remove listeners + clear refs = fix leak [OK]
Hint: Always remove event listeners and clear stored references [OK]
Common Mistakes:
Assuming Node.js auto-cleans listeners
Using emitter.once without removing listeners
Using global variables increases leaks
5. You have a Node.js app caching user sessions in a global object. Over time, memory usage grows unexpectedly. Which combined approach best prevents this memory leak?
hard
A. Use global variables for caching without cleanup
B. Store all sessions indefinitely to avoid losing data
C. Implement session expiration and remove expired sessions from cache regularly
D. Avoid using any caching to prevent leaks
Solution
Step 1: Understand the problem with indefinite caching
Storing sessions forever causes the cache to grow without limit, leading to memory leaks.
Step 2: Apply session expiration and cleanup
Removing expired sessions regularly frees memory and prevents leaks while keeping useful data.
Final Answer:
Implement session expiration and remove expired sessions from cache regularly -> Option C
Quick Check:
Expire and clean cache to prevent leaks [OK]
Hint: Expire and clear cached data regularly to avoid leaks [OK]