0
0
Node.jsframework~10 mins

Heap snapshot for memory leaks in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the module used to take heap snapshots in Node.js.

Node.js
const heapdump = require('[1]');
Drag options to blanks, or click blank then click option'
Aheapdump
Bhttp
Cfs
Dexpress
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fs' or 'http' instead of 'heapdump'.
Forgetting to install the 'heapdump' module before requiring it.
2fill in blank
medium

Complete the code to trigger a heap snapshot save with a filename.

Node.js
heapdump.writeSnapshot('[1]', (err, filename) => {
  if (err) console.error(err);
  else console.log('Snapshot saved to', filename);
});
Drag options to blanks, or click blank then click option'
Aleak.heapsnapshot
Bsnapshot.heapsnapshot
Cmemory.heapsnapshot
Dheap.heapsnapshot
Attempts:
3 left
💡 Hint
Common Mistakes
Using filenames without the '.heapsnapshot' extension.
Using unrelated names like 'leak.heapsnapshot' without context.
3fill in blank
hard

Fix the error in the code to correctly listen for a signal to trigger a heap snapshot.

Node.js
process.on('[1]', () => {
  heapdump.writeSnapshot();
});
Drag options to blanks, or click blank then click option'
ASIGTERM
BSIGUSR2
CSIGKILL
DSIGINT
Attempts:
3 left
💡 Hint
Common Mistakes
Using SIGINT or SIGTERM which terminate the process.
Using SIGKILL which cannot be caught.
4fill in blank
hard

Fill both blanks to create a heap snapshot filename with a timestamp.

Node.js
const filename = `heap-[1]-[2].heapsnapshot`;
Drag options to blanks, or click blank then click option'
ADate.now()
Bnew Date()
Cprocess.pid
DMath.random()
Attempts:
3 left
💡 Hint
Common Mistakes
Using new Date() directly in filename without formatting.
Using Math.random() which is not descriptive.
5fill in blank
hard

Fill all three blanks to write a heap snapshot and log the filename with error handling.

Node.js
heapdump.writeSnapshot([1], ([2], [3]) => {
  if ([2]) console.error([2]);
  else console.log('Snapshot saved:', [3]);
});
Drag options to blanks, or click blank then click option'
A'snapshot.heapsnapshot'
Berr
Cfilename
D'memory.heapsnapshot'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up error and filename parameters in the callback.
Using incorrect filename strings.