DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
← From Beginner to Expert in Fundamentals of Frontend
  • How the Web Works
  • Browsers: The Engines
  • Networking & Latency
  • HTTPS & TLS Handshake
  • CDNs & Edge Computing
  • WebSockets & WebRTC
  • RESTful Architecture
  • GraphQL Architecture
  • CORS (Cross-Origin Resource Sharing)
  • Load Balancing
  • Critical Rendering Path Deep Dive
  • DOM vs. CSSOM Construction
  • The Rendering Engine: Layout, Paint, Compositing
  • JavaScript Engine: V8 Internals
  • The Event Loop: Macro vs. Micro Tasks
  • Garbage Collection
  • Browser Storage Architecture
  • Service Workers & PWAs
  • Web Workers: Multi-Threading
← From Beginner to Expert in Fundamentals of Frontend

Garbage Collection

15 min read
Mark-and-SweepReference CountingMemory LeaksWeakRefWeakMap

Garbage Collection in JavaScript

JS automatically frees memory that is no longer reachable. The primary algorithm is Mark-and-Sweep.

Mark-and-Sweep

  1. Start from roots (global object, call stack variables)
  2. Mark all reachable objects
  3. Sweep (free) all unmarked objects

Common Memory Leaks

  • Forgotten event listeners
  • Closures holding large objects
  • Detached DOM nodes
  • Global variables
  • Timers (setInterval without clearInterval)

WeakRef and WeakMap

WeakRef holds a reference that does not prevent garbage collection. WeakMap keys are weakly held — perfect for caches.

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.