QA
Senior LevelNode JS

How does V8 Garbage Collection work (Young Generation Scavenger vs Old Generation Mark-Sweep-Compact)?

❓ Interview Prompt: Explain V8 Generational Garbage Collection: Young Generation (Semi-Space Scavenger / Cheney algorithm) vs Old Generation (Mark-Sweep-Compact), GC pauses, and tuning `--max-old-space-size`.

V8 manages memory using Generational Garbage Collection based on the Weak Generational Hypothesis (most objects die young!):

1
Young Generation (New Space): Stores newly allocated objects. Managed by Scavenger (Cheney algorithm) copying live objects between From-Space and To-Space (Fast, minimal pause).
2
Old Generation (Old Space): Holds long-lived objects promoted from New Space. Managed by Mark-Sweep-Compact (Full GC pause).

Tuning: Use '--max-old-space-size=4096' to expand V8 heap capacity for high-RAM applications.

Code Implementation Example

javascript
// Monitor GC statistics and memory metrics
import v8 from 'node:v8';

const heapStats = v8.getHeapStatistics();
console.log('Heap Size Limit (MB):', (heapStats.heap_size_limit / 1024 / 1024).toFixed(2));
console.log('Total Available Size (MB):', (heapStats.total_available_size / 1024 / 1024).toFixed(2));
💡Inspecting V8 engine heap memory statistics.
🎯 Interview Key Takeaway

Tune --max-old-space-size to prevent aggressive V8 Mark-Sweep GC pauses under heavy memory workloads.

#V8#Garbage Collection#Scavenger#Mark-Sweep#Senior
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?