skip to content
Alvin Lucillo

Garbage collector

/ 1 min read

💻 Tech

Garbage collectors are a built-in feature of Go, so we don’t have to release the resources ourselves. However, leaving resources behind or making inefficient allocations may cause internal latencies, contributing to the slow performance of the application. This is where mechanical sympathy comes in. It involves being mechanically sympathetic to the garbage collector. One way to increase mechanical sympathy is to understand things under the hood.

Garbage collection is done in 3 phases:

  1. Mark start — when the heap threshold is reached, garbage collector stops all goroutines, essentially doing an STW or Stop-The-World event. At this phase, no application goroutines are running.
  2. Marking — garbage collector identifies objects that are in use and those that are not not by marking them
  3. Mark termination — garbage collector lets the application goroutines back on the processor.

Garbage collectors actually don’t release resources; they are just marking points in the memory. It’s the allocator that performs resource release.