0
0
Node.jsframework~20 mins

Heap snapshot for memory leaks in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Heap Snapshot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What does a heap snapshot primarily show in Node.js?

When you take a heap snapshot in Node.js, what is the main information it provides?

AThe list of all running event loop callbacks
BThe CPU usage statistics of the Node.js process
CA detailed view of memory allocations and object references at a point in time
DThe network requests currently open by the application
Attempts:
2 left
💡 Hint

Think about what 'heap' means in programming memory terms.

component_behavior
intermediate
1:30remaining
What happens when you compare two heap snapshots in Node.js?

You take two heap snapshots at different times during your Node.js app run. What useful information can you get by comparing them?

AYou find the exact line of code causing a syntax error
BYou get the CPU usage difference between the two times
CYou get a list of all functions executed between the snapshots
DYou can see which objects have increased in number or size, indicating possible leaks
Attempts:
2 left
💡 Hint

Think about what memory leaks look like over time.

🔧 Debug
advanced
2:00remaining
Why does this heap snapshot show many detached DOM nodes in a Node.js app using jsdom?

You use jsdom in your Node.js app and take a heap snapshot. It shows many detached DOM nodes still in memory. What is the most likely cause?

AEvent listeners or references to DOM nodes are not removed, preventing garbage collection
BHeap snapshots cannot capture DOM nodes, so this is a false positive
CThe Node.js version does not support garbage collection
DThe app is running out of CPU, causing memory to freeze
Attempts:
2 left
💡 Hint

Think about what keeps objects alive in memory.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly triggers a heap snapshot in Node.js using the inspector module?

Choose the correct code to programmatically take a heap snapshot in Node.js.

Node.js
const inspector = require('inspector');
const session = new inspector.Session();
session.connect();
// Take heap snapshot here
Asession.takeHeapSnapshot(() => { console.log('Snapshot taken'); });
Bsession.post('HeapProfiler.takeHeapSnapshot', null, (err, res) => { if (err) console.error(err); else console.log('Snapshot taken'); });
Cinspector.takeHeapSnapshot(() => { console.log('Snapshot taken'); });
Dsession.post('Heap.takeSnapshot', () => { console.log('Snapshot taken'); });
Attempts:
2 left
💡 Hint

Check the exact method name and parameters for the inspector session.

state_output
expert
1:30remaining
What is the output of this Node.js heap snapshot analysis code snippet?

Given the following code snippet that analyzes a heap snapshot JSON, what will be the value of leakedObjectsCount?

Node.js
const snapshot = {
  nodes: [
    { id: 1, type: 'Object', retainedSize: 100 },
    { id: 2, type: 'Object', retainedSize: 0 },
    { id: 3, type: 'Object', retainedSize: 50 },
    { id: 4, type: 'String', retainedSize: 0 }
  ]
};

const leakedObjectsCount = snapshot.nodes.filter(n => n.type === 'Object' && n.retainedSize > 0).length;
A2
B3
C1
D4
Attempts:
2 left
💡 Hint

Count only objects with retainedSize greater than zero.