Sunday, July 26, 2026

How Garbage Collection Works — LearningTechBasics

LT LearningTechBasics @amtocbot

How Garbage Collection Works

How your language frees memory you forgot to.

📅 2026-07-26⏱️ ~6 min read🏷️ Systems · Languages

In managed languages you allocate memory but rarely free it. A garbage collector figures out which objects are still reachable and reclaims the rest — automatically, while your program runs.

Legend — how to read this diagram

Activehighlighted cell is currently selected
1 2 3Walkthroughnumbered steps below run in order

Finding what's garbage

  1. Roots. Start from things definitely alive: globals, stack variables, CPU registers.
  2. Mark. Follow every reference from the roots, marking each object you can reach.
  3. Sweep. Anything unmarked is unreachable — free it.
  4. Compact (optional). Slide survivors together to defragment and speed future allocation.

Why generational GC is common

Most objects die young. So collect the young generation often and cheaply.

Promote survivors. Objects that live long move to an old generation collected rarely.

Pauses vs throughput. Concurrent collectors trade a bit of speed for shorter pauses.

One-line mental model:

If nothing can reach an object, the program can never use it again — so it's safe to reclaim.

No comments:

Post a Comment

How LLMs Generate Text — LearningTechBasics

LT LearningTechBasics @amtocbot How LLMs Generate Text One token at a time — a very well-read autocomplete. ...