0
0
Node.jsframework~20 mins

Common memory leak patterns in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Memory Leak Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2: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?
AAdding event listeners inside a function without removing them when no longer needed
BUsing async/await to handle asynchronous code
CDeclaring variables with const instead of let
DUsing setTimeout with a delay of zero milliseconds
Attempts:
2 left
💡 Hint
Think about what happens if event listeners keep accumulating over time.
component_behavior
intermediate
2: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?
AMemory usage fluctuates randomly without any pattern
BMemory usage stays constant because global variables are optimized by the engine
CMemory usage increases over time because the objects remain referenced globally
DMemory usage decreases automatically as Node.js garbage collects global variables
Attempts:
2 left
💡 Hint
Consider how garbage collection works with references.
🔧 Debug
advanced
3: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());
}
AThe closure keeps a reference to largeData, preventing it from being garbage collected
BThe array largeData is too large to be stored in memory
CThe console.log statement causes memory to leak
DThe for loop runs too many times causing a stack overflow
Attempts:
2 left
💡 Hint
Think about what the returned function remembers from its creation context.
📝 Syntax
advanced
2:00remaining
Identifying incorrect use of timers causing leaks
Which option shows a timer usage pattern that will cause a memory leak in Node.js?
AsetTimeout(() => { /* some code */ }, 1000); // runs once
BsetInterval(() => { /* some code */ }, 1000); // never cleared
CclearInterval(timerId); // clears interval properly
DsetImmediate(() => { /* some code */ }); // runs once immediately
Attempts:
2 left
💡 Hint
Consider what happens if intervals are never stopped.
state_output
expert
3: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?
AMemory usage will cause a crash immediately
BMemory usage will stay high because the arrays are still referenced elsewhere
CMemory usage will increase because setting cache to null creates new objects
DMemory usage will drop because cache is set to null and objects become unreachable
Attempts:
2 left
💡 Hint
Think about what happens when references are removed.