DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. JavaScript Memory Management: V8 Garbage Collection Deep Dive
XLinkedInReddit
MediumFrontend Engineering

JavaScript Memory Management: V8 Garbage Collection Deep Dive

D
DevPrep Team
February 10, 2026·2 min read·0
Table of Contents
  • V8 Memory Structure
  • Scavenge (Minor GC)
  • Mark-Sweep-Compact (Major GC)
  • Common Memory Leaks
  • Debugging Memory Issues

Understanding how V8 manages memory helps you write code that doesn't leak. Here's the deep internals.

V8 Memory Structure

V8 Heap
├── New Space (Young Generation) — ~1-8MB
│   ├── Semi-space A (From-space)
│   └── Semi-space B (To-space)
└── Old Space (Old Generation) — up to GBs
    ├── Old Pointer Space (objects with pointers)
    ├── Old Data Space (raw data, strings, numbers)
    ├── Large Object Space (objects > 256KB)
    ├── Code Space (compiled code)
    └── Map Space (hidden classes)

Scavenge (Minor GC)

Fast collection for the Young Generation. Uses Cheney's algorithm with two semi-spaces.

  1. New objects allocate in From-space
  2. When From-space is full, live objects are copied to To-space
  3. From-space and To-space swap roles
  4. Objects surviving two scavenges are promoted to Old Space

Scavenge is fast (1-2ms) because most objects die young (the generational hypothesis).

Mark-Sweep-Compact (Major GC)

For the Old Generation. Three phases:

  1. Mark: Starting from roots (global object, stack), traverse all reachable objects
  2. Sweep: Free memory of unmarked objects
  3. Compact: Move surviving objects to eliminate fragmentation

Common Memory Leaks

// 1. Forgotten event listeners
element.addEventListener("click", handler);
// element removed from DOM but handler still references it

// 2. Closures retaining large scopes
function process() {
  const bigData = loadHugeDataset();
  return function summary() {
    return bigData.length; // bigData stays in memory!
  };
}

// 3. Detached DOM trees
const elements = [];
function addElement() {
  const div = document.createElement("div");
  document.body.appendChild(div);
  elements.push(div); // Array keeps reference even after DOM removal
}

// 4. Global variables
function leak() {
  accidental = "I'm global!"; // Missing var/let/const
}

Debugging Memory Issues

  • Chrome DevTools Memory tab: Take heap snapshots, compare to find leaks
  • Allocation Timeline: See where memory is allocated over time
  • Performance Monitor: Watch JS heap size in real-time
  • --max-old-space-size: Increase Node.js heap limit for large datasets

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • V8 Memory Structure
  • Scavenge (Minor GC)
  • Mark-Sweep-Compact (Major GC)
  • Common Memory Leaks
  • Debugging Memory Issues

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.