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 your program creates takes up a little bit of memory. If nothing ever cleaned that memory up, a long-running program would eventually use more and more memory until it crashed. JavaScript handles this automatically using a process called garbage collection: it periodically looks for data that nothing in your program can reach anymore, and frees that memory.
Explain like I'm 10
Think of memory like a warehouse, and garbage collection like a janitor who periodically checks for boxes with no one holding the claim ticket anymore — 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 collectionA 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();As long as setInterval keeps running, its callback (and everything it closes over, including element) stays reachable — forgetting to call clearInterval is one of the most common real-world memory leaks.
How it works
JavaScript's garbage collector uses a strategy called "mark and sweep": starting from things it knows are always reachable (like global variables and anything currently running), it walks through every reference it can find, marking each reachable object. Anything left unmarked afterward — unreachable from anywhere your code could still get to — gets swept away and its memory reused.
Why does it exist?
Manually tracking and freeing memory (as some other languages require) is tedious and a common source of serious bugs — using memory after it's freed, or forgetting to free it at all. Automatic garbage collection removes that entire category of mistakes from everyday JavaScript code.
When to use it
You don't manually trigger garbage collection — it's automatic. Where this becomes actively relevant is when you're deliberately clearing references to large objects you no longer need (setting a variable to null), or diagnosing a "memory leak" where memory usage keeps climbing over time.
When not to use it
Don't obsessively null out every local variable "just in case" — local variables are automatically freed once a function returns and nothing else references them. Focus on real leaks: forgotten event listeners, timers that are never cleared, and references held in long-lived caches or closures.
Common mistakes
Leaving event listeners or timers (
setInterval) running on elements/objects that are otherwise done being used, keeping them reachable forever.Storing ever-growing data in a long-lived cache or array with no eviction, slowly consuming more and more memory.
Assuming JavaScript frees memory the instant a variable goes out of scope — garbage collection runs periodically, not necessarily immediately.
Practice exercises
- Easy:
Explain, in your own words, why setting a variable to
nullcan help release memory. - Medium:
Describe a scenario where a forgotten
setIntervalcould cause a memory leak, and how you'd fix it. - 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.