Hiprup

What is garbage collection in JavaScript?

Garbage collection is JavaScript automatically freeing memory used by values that are no longer reachable.

  • Reachability — a value is kept while it can still be reached from a root (globals, current variables, etc.).

  • Mark-and-sweep — the engine marks reachable values and sweeps away the rest.

Leaks still happen: common causes are forgotten timers, lingering event listeners, and references trapped in closures or globals.

Know the mark-and-sweep algorithm (root-based reachability). Name 4 common leak sources: event listeners, closures, detached DOM nodes, and forgotten timers.

WeakMap/WeakSet for leak-free caching is the advanced point.

What is garbage collection in JavaScript? | Hiprup