Back to Hub

Go GC Latency vs. Heap Viz.

Visualize how Go's concurrent garbage collector handles 'Stop-the-World' (STW) latency across different heap sizes and allocation rates.

## High Performance Go: Mastering the GC

Go is famous for its low-latency garbage collector. Unlike Java's G1 or ZGC which can have longer 'Stop-the-World' (STW) pauses, Go focuses on concurrent marking to keep pauses under 1 millisecond for most heap sizes. However, this performance comes with a 'CPU Tax'.

### The GOGC Variable

The `GOGC` environment variable controls the GC's aggressiveness. A value of 100 (default) means the GC will run when the heap doubles in size. Setting it to 50 makes the GC run more often (saves memory, but uses more CPU). Setting it to 1000 makes it run less often (uses more memory, but saves CPU cycles).

### The Memory Limit (Soft Cap)

In modern Go (1.19+), we have `GOMEMLIMIT`. This allows you to set a hard target for memory usage. The GC will adjust its behavior to stay under this limit, which is crucial for preventing Out-of-Memory (OOM) kills in Kubernetes containers.

### FAQ

**Q: What is a 'Stop-the-World' pause?**
A: It's the tiny fraction of time where the runtime suspends all application goroutines to finish the GC cycle (usually just the 'Stack Scan' and 'Mark Termination' phases). Go aims for <1ms STW pauses.

**Q: Why does my Go app use 2x more memory than it should?**
A: That's the default behavior of `GOGC=100`. The runtime waits for the heap to double before cleaning. If you are memory-constrained, lower your GOGC or set a GOMEMLIMIT.

**Q: What is 'Pacer'?**
A: The Go GC Pacer is an internal algorithm that predicts when the CPU needs to start the next GC cycle so that it finishes exactly before the application hits the memory threshold. It's a highly sophisticated feedback loop.