Challenge - 5 Problems
Memory Leak Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Identifying memory leaks from event listeners
In Node.js, which of the following patterns is most likely to cause a memory leak related to event listeners?
Attempts:
2 left
💡 Hint
Think about what happens if event listeners keep accumulating over time.
✗ Incorrect
Adding event listeners repeatedly without removing them causes the listeners to accumulate in memory, leading to leaks.
❓ component_behavior
intermediate2:00remaining
Effect of global variables on memory usage
What happens to memory usage in a Node.js application if large objects are stored in global variables and never cleared?
Attempts:
2 left
💡 Hint
Consider how garbage collection works with references.
✗ Incorrect
Objects referenced by global variables are never freed, causing memory to grow as more objects accumulate.
🔧 Debug
advanced3:00remaining
Diagnosing memory leak from closures
Examine the following code snippet. What is the main cause of the memory leak?
Node.js
function createHandler() {
const largeData = new Array(1000000).fill('data');
return function handler() {
console.log(largeData[0]);
};
}
const handlers = [];
for (let i = 0; i < 1000; i++) {
handlers.push(createHandler());
}Attempts:
2 left
💡 Hint
Think about what the returned function remembers from its creation context.
✗ Incorrect
The returned handler function forms a closure that holds onto the largeData array, so none of these arrays can be freed while handlers exist.
📝 Syntax
advanced2:00remaining
Identifying incorrect use of timers causing leaks
Which option shows a timer usage pattern that will cause a memory leak in Node.js?
Attempts:
2 left
💡 Hint
Consider what happens if intervals are never stopped.
✗ Incorrect
Using setInterval without clearing it causes the callback to run forever, potentially holding references and leaking memory.
❓ state_output
expert3:00remaining
Memory usage after removing references
Given the following code, what will be the approximate memory usage behavior after running it?
Node.js
let cache = {};
function addToCache(key) {
cache[key] = new Array(1000000).fill('x');
}
for (let i = 0; i < 10; i++) {
addToCache(i);
}
// Now clear cache
cache = null;
// What happens next?Attempts:
2 left
💡 Hint
Think about what happens when references are removed.
✗ Incorrect
Setting cache to null removes references to the large arrays, allowing garbage collection to free memory.