Skip to content
BENIBIX Learn

Garbage Collection

Also known as GC, Garbage Collector, Automatic Memory Management

The language finds memory no one uses now. Then it frees it.

Think of it like

Think of it like a cleaner going room to room. They throw out what nobody uses.

Example

Lucy's JavaScript function builds a big list, uses it and returns. Nothing points at the list now. The garbage collector frees it a moment later.

Why it matters

It gets rid of a whole kind of bug, like freeing the same memory twice. So most big languages have it.

Definition

Garbage collection is when the language frees memory once nothing uses it. A part of the runtime, the garbage collector, looks for data that no part of the program can reach. It then gives that memory back so it can be used again. Java, C#, Go, JavaScript and Python all work this way. The cost is extra work in the background, and sometimes short pauses.

Common mistake

Thinking it stops every memory leak. It cannot free data your code still points at, even by mistake.

Key takeaways

  1. 1
    Finds unreachable data. Nothing points at it.
  2. 2
    Frees it for you. No free call needed.
  3. 3
    Costs some time. Work and short pauses.

Compare