How garbage collection works in JavaScript

The JavaScript garbage collector runs automatically, keeps track of memory locations, and determines which memory locations are free (i.e., safe for reuse). The garbage collector uses the mark and sweep algorithm with some optimizations.

Mark and Sweep algorithm

Reachable objects refer to those objects that are accessible and stored in the memory. A root refers to objects that are inherently reachable, these​ include:

  • global variables
  • local variables of the current function
  • parameters of the current function

Starting from the root, the garbage collector traverses the objects and marks them as visited. The objects which are unreachable from the roots are unmarked and considered garbage (memory reserved for these objects is later freed).

Garbage collector in action

let fruit = {
  name: "Orange",
  weight: 130,
};
let orange = fruit;

Fruit contains a reference to the object:

svg viewer
fruit = null;

fruit no longer contains a reference to the object. However, it is still reachable from orange and cannot be treated as garbage.

svg viewer
orange = null;

orange no longer contains a reference to the object. The object is now unreachable and the memory that it occupied will be declared safe for reuse by the garbage collector.

svg viewer
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved