Memory Management

How JavaScript decides when a piece of data is no longer needed and can be cleaned up.

What is it?

Every variable, object and function takes up some memory. If nothing cleaned it up, a long-running program would keep growing until it crashed. JavaScript handles this automatically with garbage collection: it periodically finds data your program can no longer reach and frees it.

Explain like I'm 10

Think of memory as a warehouse and garbage collection as a janitor who checks for boxes nobody holds a claim ticket for. If nobody can reach a box, it's safe to throw out and reuse the space.

Examples

Reachability determines what stays in memory

let user = { name: "Amara" };
// user is reachable — the object stays in memory

user = null;
// nothing references the { name: "Amara" } object anymore
// it becomes eligible for garbage collection

A common leak: a forgotten timer

function startPolling(element) {
  const id = setInterval(() => {
    element.textContent = new Date().toLocaleTimeString();
  }, 1000);

  return () => clearInterval(id); // caller must call this to stop it
}

const stopPolling = startPolling(document.querySelector("#clock"));
// ...later, when the clock is no longer needed:
stopPolling();

While setInterval runs, its callback (and everything it closes over, including element) stays reachable. Forgetting clearInterval is one of the most common real-world memory leaks.

How it works

The garbage collector uses "mark and sweep". It starts from things that are always reachable (like global variables and anything currently running), follows every reference it can find, and marks each object it reaches. Anything left unmarked can't be reached by your code, so it's swept away and its memory reused.

Why does it exist?

Tracking and freeing memory by hand (as some languages require) is tedious and causes serious bugs: using memory after freeing it, or never freeing it. Garbage collection removes that whole class of mistakes from everyday JavaScript.

When to use it

You never trigger garbage collection yourself. It matters when you clear references to large objects you no longer need (setting a variable to null), or when you're diagnosing a "memory leak" where memory keeps climbing.

When not to use it

Don't null out every local variable "just in case". Locals are freed once the function returns and nothing else references them. Focus on real leaks: forgotten event listeners, timers never cleared, and references held in long-lived caches or closures.

Common mistakes

  • Leaving event listeners or timers (setInterval) running on elements or objects you're done with, keeping them reachable forever.

  • Storing ever-growing data in a long-lived cache or array with no eviction, so memory keeps growing.

  • Assuming memory is freed the instant a variable goes out of scope. Garbage collection runs periodically, not necessarily right away.

Practice exercises

  1. Easy:

    Explain, in your own words, why setting a variable to null can help release memory.

  2. Medium:

    Describe a scenario where a forgotten setInterval could cause a memory leak, and how you'd fix it.

  3. Hard:

    Explain why a closure that captures a large object can unintentionally keep that object in memory long after it's needed.

Interview questions

What is garbage collection?

The automatic process JavaScript uses to find and free memory used by objects that are no longer reachable by the program.

What makes an object eligible for garbage collection?

Nothing in the program can reach it anymore — no variable, closure, or reachable structure still holds a reference to it.

What is a memory leak in JavaScript, given it has garbage collection?

Memory that's technically still reachable (so it won't be collected) but is no longer actually needed — commonly caused by forgotten timers, event listeners, or ever-growing caches.