Is Go's GC generational or compacting? What is the "Green Tea" GC?
Question 339MediumGo 1.22 to 1.25
Neither. Go's GC does not move objects, so it never compacts, and it has no generations. Reasons given by the Go team:
- Escape analysis already keeps many short-lived values on the stack, which weakens the generational hypothesis for the heap.
- Size-class segregated allocation keeps fragmentation low without compaction.
- Not moving objects keeps cgo,
unsafeand interior pointers simple. - Generational schemes need barriers on every pointer write all the time, not only during marking. Experiments showed they did not pay off.
Green Tea GC was experimental in Go 1.25 (GOEXPERIMENT=greenteagc) and became the default in Go 1.26. It keeps the same tri-color algorithm but changes how marking walks memory. Instead of chasing individual objects around the heap, it queues whole spans/pages and scans the marked small objects within each one together. That gives much better cache locality and makes vector instructions usable, cutting GC CPU cost by roughly 10-40% on many workloads. It does not change semantics: pauses, GOGC and GOMEMLIMIT behave the same.
Gotcha: a candidate who says "Go uses a generational GC like the JVM" is showing a red flag.
More on Memory, GC & Runtime Internals
- Q337What is a write barrier and which one does Go use?
- Q338Walk through the phases of a GC cycle. Where are the stop-the-world pauses?
- Q340What does GOGC control, exactly? What happens with GOGC=off, 50 or 200?
- Q341What is GOMEMLIMIT and how would you set it in a container?
- Q342How do you read a line of
GODEBUG=gctrace=1output? - Q343Why is the process RSS much larger than the heap in use? How does Go return memory to the OS?