Skip to content

Memory Management and Garbage Collection

Stack vs heap, how garbage collection actually works, generational GC in V8, memory leak patterns and detection, and preventing the leaks that crash production tabs after 30 minutes.

1
Stack, Heap, and Memory Layout
advanced

How JavaScript organizes memory at runtime. The stack stores execution frames and primitives with LIFO discipline. The heap stores objects and closures with dynamic allocation. Understanding this split explains why closures are expensive and why stack overflows happen.

13 min read
2
Value Types vs Reference Types
advanced

Primitives are copied by value — you get an independent copy. Objects are copied by reference — you get a pointer to the same data. This single distinction causes more bugs than any other JavaScript concept. Master it with shallow copy, deep copy, and structural sharing.

13 min read
3
Garbage Collection and Reachability
advanced

The garbage collector's only question: can this object be reached from any root? If yes, it lives. If no, it dies. Understand mark-and-sweep, the root set, why reference counting fails for circular structures, and how to reason about what the GC can and cannot reclaim.

10 min read
4
Generational GC in V8
advanced

V8 splits the heap into Young and Old generations based on a powerful observation: most objects die young. The Scavenger collects the Young Generation with a fast semi-space copying algorithm. Objects that survive get promoted to the Old Generation, where Mark-Sweep-Compact handles long-lived data.

13 min read
5
Incremental, Concurrent, and Parallel GC
advanced

Stop-the-world pauses kill user experience. V8 fights back with incremental marking (break work into chunks), concurrent marking (run on background threads), parallel scavenging (multiple threads for copying), and tri-color marking (track progress safely). Understand the techniques that keep GC pauses under 5ms.

14 min read
6
Memory Leak Patterns and Causes
advanced

The 5 classic memory leaks in JavaScript: forgotten event listeners, detached DOM trees, closures over large scopes, unbounded collections, and forgotten timers. Real production code for each pattern, with the exact fix. These are the leaks that crash browser tabs after 30 minutes.

14 min read
7
WeakMap, WeakSet, WeakRef, and FinalizationRegistry
advanced

Weak references don't prevent garbage collection. WeakMap stores metadata about objects without leaking them. WeakSet tracks membership without ownership. WeakRef builds caches that automatically evict. FinalizationRegistry provides cleanup hooks. Master the tools for GC-friendly data structures.

14 min read
8
ArrayBuffer, Typed Arrays, and Binary Memory
advanced

When JavaScript's automatic memory management isn't enough. ArrayBuffer gives you raw binary access. Typed arrays (Uint8Array, Float32Array) give you views into that binary data. SharedArrayBuffer enables multi-threaded memory sharing. Master the primitives for binary protocols, image processing, audio, WebAssembly interop, and high-performance computation.

14 min read
9
Memory Budgets and Leak Prevention Strategies
advanced

Proactive memory management: set budgets per component, pool objects in hot paths, clean up subscriptions with useEffect returns and AbortController, and build architecture that prevents leaks by construction. The practices that separate apps that run for 30 minutes from apps that run for 30 hours.

14 min read
10
Quiz: Find the Leak
advanced

7 real-world code snippets with hidden memory leaks. Identify the leak, explain why it happens at the GC level, and provide the fix. Each scenario tests a different leak pattern from the module. Detailed explanations reveal the full retaining path.

13 min read