[JavaScript]PUBLISHED 2026-11-13//16 min read//VERIFIED 2026-11-24

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.

EXECUTIVE ARCHITECTURAL OVERVIEW

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).

SECTION 01 //

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.

v8-gc-profiling.js
// 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);
  }
}
Enabling V8 GC tracing flags and profiling heap allocation in Node.js.
SECTION 02 //

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.

Architectural Comparison: Anti-Pattern vs Recommended Practice
[ANTI-PATTERN] High-Frequency Object Allocation in Animation Loops
// 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] Reusing Mutable Vectors or Typed Arrays
// 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);
}
[RATIONALE]Eliminating allocations in hot loops prevents the New Space from filling up, reducing minor GC cycles and eliminating frame drops.
04/PRODUCTION TROUBLESHOOTING & DIAGNOSTICS
TRIAGE MATRIX
SYMPTOM 01: Process terminated with "FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory".

[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.

SuggestedFix.ts
node --max-old-space-size=4096 --inspect server.js
TECHNICAL INQUIRY MATRIX

Frequently 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.

Official References & Specifications:
Faisal Askari

Faisal Askari

Verified Author

Software Engineer & Full-Stack Developer

Personal Website

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.

PROFESSIONAL ENGINEERING BRIDGE

Node.js & V8 Runtime Performance Auditing

Experiencing sudden latency spikes, memory leaks, or GC pauses in production Node.js services?

Schedule a V8 Performance Audit
SPEC // VERIFIED ENTITY•Karachi, Pakistan (Global Remote)
contact@faisalaskari.top
05/RELATED ENGINEERING GUIDES
CONTINUE STUDY