Repository navigation
Description
Green Tea 🍵 Garbage Collector
Authors: Michael Knyszek, Austin Clements
Updated: 15 August 2025
This issue tracks the design and implementation of the Green Tea garbage collector. As of the last update to this issue, development of Green Tea is still active. We'll produce more detailed design document once we're ready to commit to a design.
For now, Green Tea is available as an experiment in the Go 1.25 release. We don't expect any correctness issues with the experiment. It's running within Google, and we feel that it is production-ready. We encourage teams to try it out. Your feedback is instrumental to producing a good final design!
Introduction
Memory latency and bandwidth are becoming increasingly constrained as CPU clocks outpace DRAM clocks, increasing core counts offer more load on physically limited memory buses, and speed-of-light constraints necessitate increasingly non-uniform memory topologies. As a result, spatial locality, temporal locality, and topology-awareness are becoming ever more critical to high-performance systems.
Unfortunately, all of these trends are at odds with most of today’s garbage collection algorithms. Go’s garbage collector implements a classic tri-color parallel marking algorithm. This is, at its core, just a graph flood, where heap objects are nodes in the graph, and pointers are edges. However, this graph flood affords no consideration to the memory location of the objects that are being processed. As a result, it exhibits extremely poor spatial locality—jumping between completely different parts of memory—poor temporal locality—blithely spreading repeated accesses to the same memory across the GC cycle—and no concern for topology.
As a result, on average 85% of the garbage collector's time is spent in the core loop of this graph flood—the scan loop—and >35% of CPU cycles in the scan loop are spent solely stalled on memory accesses, excluding any knock-on effects. This problem is expected to only get worse as the industry trends toward many-core systems and non-uniform memory architectures.
In this document, we present Green Tea: a parallel marking algorithm that, if not memory-centric,1 is at least memory-aware, in that it endeavors to process objects close to one another together.
This new algorithm has an implementation that is ready for developers to trial on their workloads, and in this document we also present the results from evaluating this implementation against our benchmark suite. Overall, the algorithm shows a significant reduction in GC CPU costs on GC-heavy workloads.
Finally, this new marking algorithm unlocks new opportunities for future optimization, such as SIMD acceleration, which we discuss with other possible avenues of future work.
Design
The core idea behind the new parallel marking algorithm is simple. Instead of scanning individual objects, the garbage collector scans memory in much larger, contiguous blocks. The shared work queue tracks these coarse blocks instead of individual objects, and the individual objects waiting to be scanned in a block are tracked in that block itself. The core hypothesis is that while a block waits on the queue to be scanned, it will accumulate more objects to be scanned within that block, such that when a block does get dequeued, it’s likely that scanning will be able to scan more than one object in that block. This, in turn, improves locality of memory access, in addition to better amortizing per-scan costs.
Prototype implementation
In the prototype implementation of this new algorithm, the memory blocks we track are called spans. A span is always some multiple of 8 KiB, always aligned to 8 KiB, and consists entirely of objects of one size. Our prototype focuses exclusively on “small object spans”, which are exactly 8 KiB and contain objects up to 512 bytes.
A span is also the basic unit of storing heap metadata. In the prototype, each span stores two bits for each object: a gray bit and a black bit. These correspond to the tri-color abstraction: an object is black if it has been scanned, gray if it is in the queue to be scanned, and white if it has not been reached at all. In the prototype, white objects have neither bit set, gray objects have the gray bit set, and black objects have both bits set.
When scanning finds a pointer to a small object, it sets that object’s gray bit to indicate the object needs to be scanned. If the gray bit was not already set and the object’s span is not already enqueued for scanning, it enqueues the span. A per-span flag indicates whether the span is currently enqueued so it will only be enqueued once at a time. When the scan loop dequeues a span, it computes the difference between the gray bits and the black bits to identify objects to scan, copies the gray bits to the black bits, and scans any objects that had their gray bit set but not their black bit.
Limiting scope to small objects
The prototype focuses on small objects because we derive the most benefit from them. The per-scan overhead of small objects is much harder to amortize because the garbage collector spends so little time scanning each individual object. Larger objects continue to use the old algorithm.
The choice of which algorithm to use is made when scanning encounters a pointer. The span allocator maintains a bitmap with one bit for each 8 KiB page in the heap to indicate whether that page is backed by a small object span. The footprint of this fits easily into cache even for very large heaps, and contention is extremely low.
Since small object spans are always 8 KiB large and 8 KiB aligned, once the scanner knows the target of a pointer is in a small object span, it can use simple address arithmetic to find the object’s metadata within the span, thus avoiding indirections and dependent loads that seriously harm performance.
Work distribution
Go’s current garbage collector distributes work across scanners by having each scanner maintain a local fixed-sized stack of object pointers. However, in order to ensure parallelism, each scanner aggressively checks and populates global lists. This frequent mutation of the global lists is a significant source of contention in Go programs on many-core systems.
The prototype implementation has a separate queue dedicated to spans and based on the distributed work-stealing runqueues used by the goroutine scheduler. The opportunity for stealing work directly from other workers means less contention on global lists. Furthermore, by queuing spans instead of individual objects, there are far fewer items to queue and thus inherently lower contention on the queues.
Span work may be ordered several different ways. We explored several policies, including FIFO, LIFO, sparsest-first, and densest-first, random, and address-ordered. FIFO turned out to accumulate the highest average density of objects to scan on a span by the time it was dequeued for scanning.
Single-object scan optimization
If a span has only a single object to scan when it is dequeued, the new algorithm will have done more work to handle that single object than the current, object-centric algorithm does.
To bring the performance of the single-object-per-span case more in line with the current marking algorithm, we apply two tricks. First, we track the object that was marked when the span was enqueued. This object becomes the span's representative until the span is scanned. Next, we add a hit flag to the span that indicates an object was marked while the span was queued; that is, that at least two objects are marked. When scanning a span, if the hit flag is not set, then the garbage collector can directly scan the span’s representative, instead of processing the entire span.
Prototype evaluation
We evaluated the prototype implementation across a variety of benchmarks, on low-CPU-count, high-CPU-count, amd64, and arm64 Linux virtual machines. The rest of this section summarizes the results, focusing primarily on the differences in garbage collection CPU cost.
In select GC-heavy microbenchmarks (garbage, from golang.org/x/benchmarks, and binary-trees Go #2, from the Computer Language Benchmarks Game), depending on core count, we observed anywhere from a 10–50% reduction in GC CPU costs compared to the existing Go GC. The improvement generally rose with core count, indicating that the prototype scales better than the existing implementation. Furthermore, the number of L1 and L2 cache misses was reduced by half in these benchmarks.
On our bent and sweet benchmark suites, the results were more varied.
- linux/amd64 Intel Xeon Gold 6253CL Processor (Cascade Lake) (16-core VM)
- linux/amd64 Intel Xeon Platinum 8481C Processor (Sapphire Rapids) (88-core VM)
- linux/arm64 Google Axion (16-core VM)
- linux/arm64 Google Axion (72-core VM)
The results are positive overall, but include a mix of improvements and regressions.
Most benchmarks were either unaffected by the changes to the garbage collector or regressed or improved solely due to changes that had little to do with the garbage collector, such as code alignment changes. Some benchmarks regressed even though less CPU time is spent in the garbage collector. One reason for this is because the garbage collector's mark phase is active for less time, leading to less floating garbage which acts as a ballast in some benchmarks. Another reason for this is that less time spent in the garbage collector means more time spent in other scalability bottlenecks, either in the runtime or in user code, leading to a net apparent regression.
The Go compiler benchmarks appear to inconsistently show a very slight regression (0.5%). Given the magnitude and inconsistency of the regression, these benchmarks appear to be rather insensitive to this change. One hypothesis is that the occasional regression may be due to an out-of-date PGO profile, but remains to be investigated.
The tile38 benchmark shows substantial improvements across throughput, latency and memory use. In addition, it showed a 35% reduction in garbage collection overheads. This benchmark queries a local instance of a Tile38 in-memory geospatial database pre-seeded with data. Most of the heap consists of a high-fanout tree, so Green Tea is able to quickly generate large amounts of work and high densities.
The bleve-index benchmark has a heap topology that is quite difficult for Green Tea, though performance overall is a wash. Most of the heap consists of a low-fanout binary tree that is rapidly mutated by the benchmark. Green Tea struggles to generate locality in this benchmark, and half of all span scans only scan a single object. Our current hypothesis is that because of frequent tree rotations, the tree structure itself becomes shuffled across a large heap (100+ MiB). In contrast, the binary-trees benchmark does not perform rotations, so the tree layout retains the good locality of its initial allocations. This suggests that Green Tea has good locality when the application itself has good locality, unlike the current Go GC; but Green Tea, unsurprisingly, can’t create locality out of nothing. Both Linux perf and CPU profiles in the 16-core amd64 environment indicate a small ~2% regression in garbage collection overhead. Overall, the single object scan optimization was integral to making this benchmark perform well in the 16-core amd64 environment. In the 72- and 88-core environments we see a significant improvement, due to the design's improved many-core scalability. There remains a small overall regression on the 16-core arm64 environment that still needs to be investigated.
Future work
SIMD-accelerated scanning kernels
Scanning memory in larger blocks of memory unlocks the ability to apply SIMD to small objects in the garbage collector. The core idea is to generate a unique scanning kernel for each size class and use SIMD bit manipulation and permutation instructions to load, mask, swizzle, pack, and enqueue pointers. The regularity of the layout of objects in a single span and Go’s packed representation of pointer/scalar metadata for small objects both play a major role in making this feasible.
Austin Clements developed prototype AVX512-based scanning kernels that reduce garbage collection overheads by another 15–20% in the benchmarks where we already saw improvements. The prototype implementation does not currently use these kernels because they only apply to a small subset of objects at this point in time.
These SIMD kernels tend to require a higher density of objects in order to outperform sparsely scanning objects within a scan. These kernels are still being developed, so the prototype does not use them by default, and when they are enabled, it only uses SIMD scanning when a minimum density threshold is reached.
Concentrator network
Austin's original design for Green Tea used a sorting network called the concentrator network to achieve the even higher pointer density required by SIMD-based scanning, and to generate locality even for metadata operations like setting gray bits. The network was carefully designed to minimize queuing costs, so individual pointers could still be processed efficiently when sufficient scan density was unavailable.
There are two main reasons we did not pursue this direction in the short term. First, we found that even very low density can produce good results, especially with the single-object scan optimization. Second, the concentrator network is more complex to implement, as it is a greater departure from the existing algorithm. However, this design is still an avenue we plan to explore, since it is far more general and tunable.
Acknowledgements
Credit to Austin Clements for the inception of the key idea (that is, accumulating pointers on a span before scanning, which they wrote down in 2018!), initial prototyping of the concentrator network, which motivated further work, and in polishing up this text significantly.
Credit to Yves Vandriessche from Intel for providing many microarchitectural insights that were vital to making this design viable. Many of his suggestions were applied to the new core scan loop, including proper prefetching, batching subsequent pointer loads to hide their memory latency, and simpler iteration over object pointers.
Footnotes
-
"The Garbage Collection Handbook" by Richard Jones, Anthony Hosking, Eliot Moss, a canonical source for garbage collection techniques, divides parallel garbage collection algorithms into two categories, "processor-centric" and "memory-centric." Loosely, the former category consists of algorithms that aggressively balance work across processors to maximize parallelism, while the latter consists of algorithms that may not parallelize as well, but process contiguous blocks of the heap. The current marking algorithm in the Go garbage collector is firmly processor-centric. Unfortunately for us, the handbook's wisdom ends there, as it goes on to state that "[a]ll known parallel marking algorithms are processor-centric." (page 279) ↩
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
gopherbot commentedon May 2, 2025
Change https://go.dev/cl/658036 mentions this issue:
runtime: mark and scan small objects in whole spans [green tea]mknyszek commentedon May 2, 2025
How to try it out
First,
Then build your program with
GOEXPERIMENT=greenteagc go1.25rc1 build.Sending feedback
Please focus your attention on whole programs. Microbenchmarks tend to be poor representatives of garbage collection behavior in real programs. Large changes in the foundations such as these can move the needle for all sorts of reasons, unrelated to (or even inversely related to) how efficient the garbage collector actually is.
If you encounter a situation where Green Tea isn't working out for you, we'd appreciate it if you could share some details with us that would be really helpful moving forward. (This is also helpful if it is working for you.)
GODEBUG=gctrace=2, both with and without Green Tea.Note: you can use
GOEXPERIMENT=nogreenteagcto explicitly disable Green Tea at build time.For problems you encounter, please file a new GitHub issue and reference this one, or you can email me directly at
mknyszek(at)golang.orgif you're not comfortable publishing details or diagnostics publicly.For successes, post to this issue!
Thank you!
runtime: mark and scan small objects in whole spans [green tea]
gopherbot commentedon May 2, 2025
Change https://go.dev/cl/669655 mentions this issue:
main.star: add greenteagc buildersmain.star: add greenteagc builders
dominikh commentedon May 15, 2025
No discernible changeMinor improvement in CPU time for Staticcheck.CPU: AMD Ryzen 9 3950X 16-Core Processor
The attached files contain the CPU profiles and the gctrace output. I also have execution traces, but they span the whole execution and are ~50 MB each when compressed, which GitHub won't let me attach.
greentea.tar.gz
vanilla.tar.gz
perf statbefore/after courtesy of normalize-benchfmt:mknyszek commentedon May 15, 2025
Thanks for sharing! I think there is a win here, though it's not a massive improvement. I think there are some effects that might be masking a bigger win.
At least according to the
gctraceI see less overall CPU time spent in any individual GC cycle. On average around 10-15% .Also, here are the distributions of wall time spent in the mark phase:
Simultaneously, because each GC cycle is shorter, there's less floating garbage, leading to a smaller live heap. This means for the same amount of allocation work, we're going to have more GC cycles, if
GOGCis constant (which it is here). Proof of that is in the fact that with Green Tea there are 152 GC cycles, whereas the original GC only executes 139 cycles. Despite this increase in the number of cycles, the CPU profile suggests that around 10% less time is spent in the garbage collector.Here are the heap size distributions:
As for why this isn't improving overall end-to-end time much, I suspect that:
staticcheckrun in parallel or is it mostly a single goroutine?(I could probably analyze this more efficiently if I just made a tool to parse
gctraces... I've been doing quite a lot of this lately. 😅)108 remaining items
zliu5-sc commentedon Nov 4, 2025
hi team, @mknyszek thank you for the great work.
We're evaluating and planning to adopt Green Tea for our production workload, which is memory heavy (90~125GiB live heap at steady state; depending on region).
We've observed overall CPU utilization improvement, but with a caveat:
GOGCvalues; burst in traffic), we observe huge latency increase from the application, presumably from GC having to take more CPU time away from the application.the follow graph (
canarys are Green Tea;baselineis non-Green Tea) shows the contrast very well:would appreciate your thoughts @mknyszek and also hear from any other early adopters of Green Tea in a production setting if you have experienced similar trade off.
( re: per-cycle CPU usage: we didn't capture them as metrics but from gc trace logs for Green Tea is about 12%; for non-Green Tea is about 7% )
at this point, our main blocker for adopting Green Tea is that it performs well when it is able to run less frequently (based on GOGC+GOMEMLIMIT combo), but it performs worse at higher memory stress (we are not sure if Green Tea will start to thrash on sudden traffic surge)
mknyszek commentedon Nov 4, 2025
@zliu5-sc Thanks for reporting. It sounds like memory usage bumped slightly causing the speedup, but that it is slower overall.
Is this with Go 1.25 or Go at tip? There are lock contention issues with some workloads with the Go 1.25 implementation and the behavior you're seeing here reminds me a lot of those issues.
If you are indeed running Go 1.25, I would suggest trying at tip, where the issue is fixed. It's already enabled by default at tip, so no need to set any environment variables. (Try out https://golang.org/dl/gotip for doing so.)
Also, how are you measuring CPU utilization? OS metrics? Go runtime metrics? Depending on your answer, that will help identify if it's the issue I expect. (That is, if you're looking at OS-reported metrics measuring actual on-CPU time, then I suspect it is indeed lock contention at the root of things here for you. But we'll see, it's just one possible piece of evidence.)
If it persists at tip, any additional data you can share (from the list #73581 (comment)) would help a lot. Thanks.
zliu5-sc commentedon Nov 4, 2025
Thank you @mknyszek
container_memory_working_set_bytes; we run in GKE) is up, but still below GOMEMLIMIT (which we are happy about :P ); I agree with your assessment that current overall cpu utilization improvement likely came from this trade offcontainer_cpu_usage_seconds_total/ <no. of cores requested>zliu5-sc commentedon Nov 4, 2025
here's gctrace log excerpts from latest run. we run on GKE with
c3d-standard-60machines.mknyszek commentedon Nov 4, 2025
The fact that OS-reported CPU utilization doesn't change much but the gctrace shows a huge difference really does suggest to me that this is the fixed-at-tip lock contention issue. The gctrace measures CPU time as monotonic time measurements at the GC worker level, which will include off-CPU time due to lock contention. The OS-reported CPU utilizations will NOT include this off-CPU time. Hence, that suggests to me that the discrepancy is off-CPU time.
Please do try tip-of-tree. If the problem persists or there's some new problem, we'll dig deeper.
zliu5-sc commentedon Nov 4, 2025
Thanks @mknyszek I'm in the process of importing tip into our bazel toolchain. Will report back result with tip as soon as I have them. Cautiously optimistic about it!
zliu5-sc commentedon Nov 5, 2025
hi @mknyszek it seems your intuition is spot on! here's a run with Green Tea on tip:
now the gc reported cpu time is more in line with container reported cpu time, but more importantly, it seems to have fixed our latency regression at p99 (possibly due to the same contention issue during the gc cycle)
may I seek your advice on how to adopt your fix before 1.26 release? will there be a minor release on 1.25 containing the fix? should we try cherry-pick the fix into 1.25 tree?
mknyszek commentedon Nov 5, 2025
Unfortunately I don't think we'll be backporting the fix to Go 1.25 at this point, since it's just an opt-in
GOEXPERIMENTin Go 1.25. The changes are too big and too invasive to cherry-pick.My suggestion would be to either disable the
GOEXPERIMENTand wait for Go 1.26, or run Go at tip for now.zliu5-sc commentedon Nov 8, 2025
hi @mknyszek thanks again for your help. want to report back our finding:
as Go 1.26 is still a bit ways out, we experimented with cherry-picking the specific commit d7a38ad (and a few ancestors to resolve merge conflict).
for our workloads, this specific commit did indeed fix our p99 regression and still allow us to reap the benefits of Green Tea.
mknyszek commentedon Nov 19, 2025
The freeze is upon us and I'm inclined to ship what we've got. It's on by default now. We'll still have the opt-out for one more release, and we can keep iterating if/when new things come up.
Closing out this issue!