Garbage collection is a form of automatic memory management where the runtime reclaims memory that is no longer reachable by the program.

Instead of requiring the programmer to free memory manually, the runtime tracks objects and decides when they can be safely reclaimed.

Garbage collection is common in languages such as Go, Java, Javascript, Python, and Ruby.

Why people use it

Garbage collection reduces the risk of:

  • Double free bugs
  • Use-after-free bugs
  • Memory leaks caused by forgetting to release memory

Tradeoffs

Garbage collection makes memory management easier, but it can introduce runtime overhead and occasional pauses while the collector runs.