● LIVE   Breaking News & Analysis
Xpj0311
2026-05-02
Environment & Energy

Maximizing Go Performance with the Green Tea Garbage Collector: A Hands-On Tutorial

Learn to enable and evaluate the experimental Green Tea garbage collector in Go 1.25. Step-by-step instructions, benchmarking tips, and common pitfalls to reduce GC overhead by 10-40%.

Overview

Garbage collection (GC) is a double‑edged sword: it frees developers from manual memory management, but runtime overhead can slow down applications. The Go team has introduced an experimental garbage collector named Green Tea in Go 1.25. Benchmarks show that many workloads spend 10–40% less time in the GC, with production deployments at Google already reaping the benefits. This tutorial will guide you through enabling, testing, and evaluating Green Tea in your own projects. By the end, you’ll know how to leverage this new collector to reduce pause times and improve throughput, and you’ll understand how to provide valuable feedback to the Go team.

Maximizing Go Performance with the Green Tea Garbage Collector: A Hands-On Tutorial
Source: blog.golang.org

Prerequisites

  • Go 1.25 or later – Green Tea is experimental in 1.25 and will become default in 1.26. Download the latest release from go.dev/dl.
  • A Go project that allocates heap memory (most real‑world programs do). A simple web server, CLI tool, or data pipeline works well.
  • Basic familiarity with the Go build toolchain – you should know how to set environment variables and run go build.
  • Benchmarking toolsbenchstat or simple shell timers to compare before/after results.

Step‑by‑Step Instructions

1. Enable Green Tea at Build Time

Add the GOEXPERIMENT=greenteagc environment variable when building your application. There are two common ways:

# Approach A: Set per command
GOEXPERIMENT=greenteagc go build -o myapp .

# Approach B: Export in your shell (persistent for the session)
export GOEXPERIMENT=greenteagc
go build -o myapp .

Note: The environment variable must be set at build time, not at runtime. Once the binary is built, it will always use the chosen collector.

2. Verify the Build

Check which collector your binary is using by running:

go version -m myapp | grep GOEXPERIMENT

If you see GOEXPERIMENT=greenteagc, you’re good. Otherwise, rebuild with the variable set.

3. Run Your Application

Launch your binary as you normally would. No runtime flags are needed. Monitor GC activity using the GODEBUG=gctrace=1 environment variable to see detailed GC logs:

GODEBUG=gctrace=1 ./myapp

This prints lines like:

gc 1 @0.024s 2%: 0.008+0.50+0.012 ms clock, 0.016+0.20/0.40/0.20+0.024 ms cpu, 4->4->0 MB, 5 MB goal, 8 P

Pay attention to the total GC CPU time (the first percentage and the numbers after cpu).

4. Benchmark With and Without Green Tea

To measure improvement, build two binaries – one with the default GC and one with Green Tea – and compare them under identical workload.

  1. Build the baseline binary (without GOEXPERIMENT):
    go build -o myapp-default .
  2. Build the Green Tea binary:
    GOEXPERIMENT=greenteagc go build -o myapp-greentea .
  3. Run a realistic load test against each. For example, use wrk for HTTP servers or time for a CLI tool.

Example with a web server:

# Baseline
./myapp-default &
PID=$!
wrk -t4 -c100 -d30s http://localhost:8080
kill $PID

# Green Tea
./myapp-greentea &
PID=$!
wrk -t4 -c100 -d30s http://localhost:8080
kill $PID

Record the latency percentiles and request rate. Typically, Green Tea reduces GC pauses and improves tail latency.

Maximizing Go Performance with the Green Tea Garbage Collector: A Hands-On Tutorial
Source: blog.golang.org

5. Analyze gctrace Output

For deeper analysis, capture GC traces from both runs and compare the total GC CPU time. A quick script to sum the CPU percentages:

GODEBUG=gctrace=1 ./myapp 2>&1 >/dev/null | grep 'gc ' | awk '{print $3}' | sed 's/%//' | awk '{sum+=$1} END {print sum}'

This prints the cumulative percentage of CPU spent in GC. Green Tea should show a lower number for memory‑intensive workloads.

6. Provide Feedback

If you see improvements, post your success on the existing Green Tea issue. If you encounter problems (regressions, panics, unexpected behavior), file a new issue with details about your workload, hardware, and any error messages.

Common Mistakes

  • Setting GOEXPERIMENT at runtime – The environment variable must be present during go build. Adding it at execution time has no effect.
  • Building with an older Go version – Green Tea is only available in Go 1.25+. Verify your Go version with go version.
  • Misleading benchmarks – If your workload is CPU‑bound and barely allocates, Green Tea may show no improvement. Focus on allocation‑intensive workloads.
  • Forgetting to rebuild after changing GOEXPERIMENT – The binary caches the collector choice. Always rebuild when toggling the flag.
  • Ignoring the gctrace percentage – A 1% reduction in GC time may be within noise. Run multiple iterations and use statistical tools like benchstat.

Summary

Green Tea is a promising evolution of the Go garbage collector that reduces GC overhead significantly for many real‑world programs. By following this tutorial, you’ve learned how to enable it at build time, verify its usage, benchmark your application, and interpret the results. Remember that it is experimental in Go 1.25 and feedback is essential for its path to becoming the default in Go 1.26. Try it on your own projects, share your findings, and help the Go community deliver faster, more efficient applications.