Memory Management & Garbage Collection0%

Memory Management & Garbage Collection

Beginner12 min read•Updated: Jul 11, 2026
Study Materials

Memory Management & Garbage Collection in V8

JavaScript automatically manages memory allocation and deallocation through an automated Garbage Collector (GC). However, high-throughput backend services and complex client applications can suffer severe latency spikes ("Stop-the-World" pauses) if developers write code that fights V8's memory management engine.


1. The V8 Memory Lifecycle: Allocation, Usage, Release

Output
1. Allocation: Memory reserved on Stack (primitives) or Heap (objects)
│
2. Usage: Read and write variables in memory
│
3. Release: Garbage Collector detects unreferenced objects and frees memory
  • Stack Memory: Stores primitive values (number, boolean, symbol) and references/pointers. Super fast allocation and deallocation (LIFO).
  • Heap Memory: Stores dynamic, complex objects (objects, arrays, functions, closures). Managed by the Garbage Collector.

2. Reachability & The Mark-and-Sweep Algorithm

Modern JavaScript engines use the Mark-and-Sweep algorithm based on the concept of Reachability:

  • A value is reachable if it is accessible directly or indirectly from a Root (Global window/global, local variables in current call stack).
  • Mark Phase: The GC traverses the object graph starting from roots, marking every visited object as "alive".
  • Sweep Phase: Any un-marked memory is swept and returned to the free list.
Output
Root (window)
├──► User Object [MARKED]
│ └──► Profile Object [MARKED]
│
Orphaned Node [UNMARKED - SWEPT & FREED!]

3. V8 Generational Garbage Collection

V8 divides the Heap into two main generations based on the Weak Generational Hypothesis ("Most objects die young"):

Output
┌─────────────────────────────────────────────────────────────┐
│ V8 HEAP │
├──────────────────────────────┬──────────────────────────────┤
│ YOUNG GENERATION (Nursery) │ OLD GENERATION │
│ • Newly allocated objects │ • Objects that survived │
│ • Collected by Scavenger │ multiple Scavenge cycles │
│ • Super fast (~1-2ms) │ • Collected by Major GC │
│ • Semi-space copy algorithm │ (Mark-Sweep-Compact) │
└──────────────────────────────┴──────────────────────────────┘
  1. 1
    Young Generation (Minor GC / Scavenger): Uses a two-semi-space copying collector. Very fast (~1ms). Most temporary objects are destroyed here.
  2. 2
    Old Generation (Major GC / Full Mark-Sweep): Objects that survive multiple young cycles are promoted to the Old Generation. Major GC runs less frequently, using incremental and concurrent marking to minimize UI freeze pauses.

4. Avoiding GC Pauses: Object Pooling

In high-performance gaming or real-time trading engines, creating thousands of temporary objects per second causes frequent Minor GC pauses. Object Pooling reuses pre-allocated objects instead of creating new ones:

JavaScript
class Vector2DPool {
constructor(size = 500) {
this.pool = Array.from({ length: size }, () => ({ x: 0, y: 0 }));
}
 
acquire(x, y) {
const vec = this.pool.pop() || { x: 0, y: 0 };
vec.x = x;
vec.y = y;
return vec;
}
 
release(vec) {
this.pool.push(vec); // Return to pool for reuse! Zero new heap allocation!
}
}

Interactive Quiz

Practice Quiz

Question 1
What concept defines whether an object is retained or collected during Mark-and-Sweep garbage collection?
Question 2
What is the "Weak Generational Hypothesis" that underpins V8's memory architecture?
Question 3
How does V8 handle young vs. old generation garbage collection?
Question 4
Why can creating thousands of short-lived objects inside an animation or physics loop cause visual stutter?
Question 5
What design pattern pre-allocates a fixed set of reusable objects to avoid runtime heap allocations and GC spikes?
Next Lesson

Core Web Vitals & JavaScript Impact

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext