0
0
Node.jsframework~20 mins

Memory management best practices in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Memory Mastery in Node.js
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Garbage Collection in Node.js
What happens when an object in Node.js is no longer referenced by any part of the program?
AThe object is immediately deleted from memory by the programmer.
BThe object remains in memory until the program ends.
CThe object is moved to a special memory area for later use.
DThe garbage collector automatically frees the memory used by the object.
Attempts:
2 left
💡 Hint
Think about how automatic memory cleanup works in modern programming environments.
component_behavior
intermediate
2:00remaining
Effect of Large Buffers on Memory
Consider this Node.js code snippet that creates a large buffer inside a function. What is the likely effect on memory after the function finishes if the buffer is not returned or stored elsewhere?
Node.js
function createBuffer() {
  const buf = Buffer.alloc(10 * 1024 * 1024); // 10MB buffer
  // do something with buf
}
createBuffer();
AThe buffer is eligible for garbage collection after the function ends because it is not referenced.
BThe buffer is freed only when the program exits.
CThe 10MB buffer remains in memory indefinitely, causing a memory leak.
DThe buffer is copied to a global variable automatically.
Attempts:
2 left
💡 Hint
Think about what happens to local variables after a function finishes if they are not saved.
🔧 Debug
advanced
2:00remaining
Identifying Memory Leak Cause
Which option best explains why this Node.js code causes a memory leak?
Node.js
const cache = {};
function addToCache(key, value) {
  cache[key] = value;
}

setInterval(() => {
  addToCache(Date.now(), new Array(1000000).fill('*'));
}, 1000);
AThe setInterval function is not cleared, causing multiple timers to run.
BThe cache object keeps growing without removing old entries, preventing garbage collection.
CThe array is too large to be stored in memory, causing immediate crash.
DThe Date.now() function returns the same value every second, overwriting cache keys.
Attempts:
2 left
💡 Hint
Think about what happens when you keep adding data to an object without removing anything.
📝 Syntax
advanced
2:00remaining
Correct Use of Weak References to Avoid Memory Leaks
Which code snippet correctly uses a WeakMap to store objects without preventing their garbage collection?
A
const wm = new WeakMap();
const obj = {};
wm.set(obj, 'value');
B
const wm = new WeakMap();
const obj = {};
wm.set('key', obj);
C
const wm = new WeakMap();
const obj = {};
wm.set({}, 'value');
D
const wm = new WeakMap();
wm.set('key', 'value');
Attempts:
2 left
💡 Hint
Remember that WeakMap keys must be objects, not strings or primitives.
state_output
expert
2:00remaining
Memory Usage After Manual Buffer Nullification
What is the expected behavior of the memory usage after running this Node.js code?
Node.js
let bigBuffer = Buffer.alloc(50 * 1024 * 1024); // 50MB buffer
console.log(process.memoryUsage().heapUsed);
bigBuffer = null;
setTimeout(() => {
  console.log(process.memoryUsage().heapUsed);
}, 1000);
AThe program will crash due to freeing memory manually.
BThe second memory usage log will show a significantly lower heapUsed value because the buffer is freed.
CThe second memory usage log will be the same or slightly higher because garbage collection may not have run yet.
DThe buffer remains in memory because setting to null does not affect references.
Attempts:
2 left
💡 Hint
Think about when garbage collection runs and how memory usage is reported.