When you take a heap snapshot in Node.js, what is the main information it provides?
Think about what 'heap' means in programming memory terms.
A heap snapshot captures the memory allocations and how objects reference each other at a specific moment. It helps identify memory leaks by showing what is still in memory.
You take two heap snapshots at different times during your Node.js app run. What useful information can you get by comparing them?
Think about what memory leaks look like over time.
Comparing heap snapshots shows growth in objects or retained memory, which helps identify leaks by highlighting what is not being freed.
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?
Think about what keeps objects alive in memory.
If event listeners or variables still reference DOM nodes, they cannot be freed, causing detached nodes to accumulate in memory.
Choose the correct code to programmatically take a heap snapshot in Node.js.
const inspector = require('inspector');
const session = new inspector.Session();
session.connect();
// Take heap snapshot hereCheck the exact method name and parameters for the inspector session.
The correct method is session.post with 'HeapProfiler.takeHeapSnapshot' as the command. Other options use wrong method names or calls.
Given the following code snippet that analyzes a heap snapshot JSON, what will be the value of leakedObjectsCount?
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;Count only objects with retainedSize greater than zero.
Only nodes with type 'Object' and retainedSize > 0 are counted. Nodes 1 and 3 meet this condition, so count is 2.