V8 Engine Garbage Collection Internals: Scavenger Minor GC, Major Mark-Sweep, and Memory Heap Profiling
An exhaustive exploration of generational garbage collection in Node.js and Chromium, memory heap generations, and memory leak mitigation strategies.
V8 manages memory through a generational garbage collector based on the Weak Generational Hypothesis: most objects die young. Memory is divided into the Young Generation (New Space, managed by the Scavenger collector using Cheney’s copying algorithm) and the Old Generation (Old Space, managed by the Major Mark-Sweep-Compact collector). Understanding these mechanics is essential for preventing GC pauses from degrading frame rates and backend latency.
THE PRODUCTION PROBLEM STATEMENT
Uncontrolled object allocation causes frequent garbage collection pauses (Stop-The-World events). In frontend interfaces, GC pauses cause frame drops and choppy animations; in backend Node.js microservices, they cause latency spikes and unexpected memory exhaustion (OOM crashes).
How Does V8 Partition the Memory Heap?
The V8 heap is divided into distinct spaces. The New Space (1-64MB) stores newly allocated objects and is subdivided into two semi-spaces: From-Space and To-Space.
When the New Space fills up, the Minor GC (Scavenger) copies surviving objects from From-Space to To-Space. Objects that survive two consecutive minor GCs are promoted to the Old Space.
The Old Space holds long-lived objects and is collected via the Major GC using a three-phase process: Concurrent Marking, Incremental Sweeping, and Lazy Compacting.
// Start Node with GC trace flags:
// node --trace-gc --trace-gc-verbose server.js
const v8 = require('v8');
function logHeapStats() {
const stats = v8.getHeapStatistics();
console.log({
totalHeapSizeMB: (stats.total_heap_size / 1024 / 1024).toFixed(2),
usedHeapSizeMB: (stats.used_heap_size / 1024 / 1024).toFixed(2),
heapSizeLimitMB: (stats.heap_size_limit / 1024 / 1024).toFixed(2),
physicalTotalMB: (stats.total_physical_size / 1024 / 1024).toFixed(2)
});
}
// Memory pool to prevent high-frequency object churn
class ObjectPool {
constructor(factory, size = 1000) {
this.factory = factory;
this.pool = Array.from({ length: size }, () => factory());
}
acquire() {
return this.pool.pop() || this.factory();
}
release(item) {
this.pool.push(item);
}
}How to Prevent Premature Promotion to Old Space?
Minor GC takes under 2ms because it only inspects the small New Space. However, if short-lived objects are held in closures across async ticks, they survive into Old Space.
Collecting the Old Space requires Major GC, which consumes significantly more CPU. Reusing object buffers and avoiding global cache retention prevents premature promotion.
// Anti-Pattern: Allocates objects at 60fps, triggering frequent GC pauses
function onFrame() {
const coords = { x: calculateX(), y: calculateY() }; // New object every 16ms!
render(coords);
requestAnimationFrame(onFrame);
}// Recommended: Allocate vector once and mutate properties
const coords = { x: 0, y: 0 };
function onFrame() {
coords.x = calculateX();
coords.y = calculateY();
render(coords);
requestAnimationFrame(onFrame);
}[PROBABLE CAUSE] A memory leak (unbounded Map/Set, uncleaned event listeners, or module-scoped caches) prevented Old Space objects from being collected.
[VERIFIED RESOLUTION] Capture a heap snapshot using node --inspect and Chrome DevTools to locate detached objects with large retained sizes.
node --max-old-space-size=4096 --inspect server.jsFrequently Asked Questions & Technical Inquiries
Direct, practical answers to common architectural and debugging questions.
Concurrent Marking allows worker threads to trace and mark reachable objects while JavaScript continues running on the main thread, drastically reducing main-thread pause times.
Faisal Askari
Verified AuthorSoftware Engineer & Full-Stack Developer
Faisal Askari is a software engineer with deep expertise in web systems architecture, the JavaScript runtime ecosystem, and client-side performance engineering. Over years of hands-on production engineering, he has designed and shipped enterprise dashboards, high-converting digital interfaces, and specialized full-stack applications.
Node.js & V8 Runtime Performance Auditing
Experiencing sudden latency spikes, memory leaks, or GC pauses in production Node.js services?
The JavaScript Event Loop in Depth: Microtasks, Macrotasks, and Browser Frame Budgeting
An exhaustive dissection of V8 execution contexts, microtask queue drainage, task scheduling, requestAnimationFrame, and preventing main-thread UI jank.
Migrating from Legacy Date to the JavaScript Temporal API: Immutability and Time Zone Precision
A comprehensive migration handbook for replacing the 30-year-old Date object with immutable, IANA-aware Temporal primitives.