Stop 04 · JavaScriptTopics · 29/36

Garbage Collection

Part of the Frontend Developer Roadmap

In short

You never free memory yourself in JavaScript. A garbage collector quietly clears out values nobody can reach any more. Knowing what "reachable" means helps you avoid leaks.

JavaScript's automatic process of freeing memory no longer reachable by any reference, so you don't have to manually deallocate memory like in C. Garbage collection is based on reachability rather than whether a variable 'looks unused', so lingering references can keep large object graphs alive.

The jaadu mascot as a hostel warden sweeps a pile of unclaimed items out of a room.

Think of it like this

The warden's semester-end check

At semester end, the warden checks every room. Anything a current student can claim stays. Stuff nobody can trace back to anyone gets cleared out. The garbage collector does the same: if no variable can reach a value, it gets removed.

Watch it happen

Is { name: Arjun } still reachable?

GC roots
student
topper
{ name: Arjun }

1/4Both variables point at the object. It's reachable from the roots, so it stays.

The big ideas

What you need to know

Reachability is the rule

Start from roots like global variables and the current call stack. Anything you can reach from them stays alive.

Mark and sweep

The collector marks everything reachable, then sweeps away the rest and reuses that memory.

Cycles are fine

Two objects pointing at each other still get collected if nothing else reaches them.

You don't control the timing

The engine decides when to collect. Setting a variable to null only makes the value eligible, it doesn't free it right away.

How it works

A mark-and-sweep round

  1. 1

    Start at the roots

    Globals and variables on the current call stack.

  2. 2

    Mark

    Follow every reference from the roots and mark each object reached.

  3. 3

    Sweep

    Any object left unmarked can't be used by your code, so it's removed.

  4. 4

    Reuse

    The freed memory is available for new objects.

See it in code

Walk through the code

js
1let student = { name: "Arjun" };2let topper = student;    // two references to the same object3 4student = null;          // still reachable through topper5topper = null;           // now unreachable, it can be collected6 7function makePair() {8  const a = {};9  const b = {};10  a.friend = b;11  b.friend = a;          // a cycle12  return "done";13}14makePair();              // both become unreachable after this, cycle or not

1/4Two variables, one object.

An object is kept as long as at least one path from a root leads to it. Cut every path and it becomes garbage.

Save yourself the pain

Common mistakes

Oops

Thinking obj = null instantly frees the memory.

Do this instead

It only removes one reference. The object is freed later, and only if nothing else still points to it.

Oops

Worrying that circular references leak in JavaScript.

Do this instead

Mark and sweep handles cycles. The real leaks come from references you forgot about, like a global cache.

Your mission

Reachable or not?

~15 min

Quick check

let a = {}; let b = a; a = null; Can the object be garbage collected now?

Go deeper

  1. 1Read about the 'mark-and-sweep' algorithm most engines use. Then apply the idea in a small example so the theory becomes practical.
  2. 2Understand what 'reachability' means in the context of garbage collection. Be able to explain the concept in your own words and identify where it appears in a real application.
  3. 3Learn common patterns (forgotten timers, global references) that prevent objects from being collected.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge