Bird
0
0

Which sequence correctly describes how to programmatically capture a heap snapshot in Node.js and then analyze it for retained objects?

hard📝 Application Q9 of 15
Node.js - Debugging and Profiling
Which sequence correctly describes how to programmatically capture a heap snapshot in Node.js and then analyze it for retained objects?
AUse the 'fs' module to write process.memoryUsage() output to a file and analyze it with a text editor
BCall process.memoryUsage() repeatedly and log the output to detect retained objects
CUse the 'v8' module's getHeapSnapshot() to create a stream, save it to a file, then load it in Chrome DevTools for analysis
DInvoke global.gc() and then check console logs for retained objects
Step-by-Step Solution
Solution:
  1. Step 1: Capture heap snapshot programmatically

    Use the 'v8' module's getHeapSnapshot() method which returns a readable stream.
  2. Step 2: Save the snapshot stream to a file

    Pipe the stream to a writable file stream to persist the snapshot.
  3. Step 3: Analyze the snapshot

    Open the saved snapshot file in Chrome DevTools' Memory panel to inspect retained objects and detect leaks.
  4. Final Answer:

    Use the 'v8' module's getHeapSnapshot() to create a stream, save it to a file, then load it in Chrome DevTools for analysis -> Option C
  5. Quick Check:

    Programmatic snapshot + Chrome DevTools analysis is the correct approach [OK]
Quick Trick: Stream snapshot with 'v8' and analyze in Chrome DevTools [OK]
Common Mistakes:
  • Using process.memoryUsage() for detailed leak analysis
  • Not saving the snapshot stream properly
  • Expecting console logs to show retained objects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes