Stage 3 · Build
Performance, Memory & Debugging
GC Tuning
GOGC, GOMEMLIMIT, heap goals, pacer behavior, and latency tradeoffs.
GC Overview
Go uses a concurrent, tri-color mark-and-sweep garbage collector. It runs concurrently with application code, minimizing pause times. The GC targets low latency over high throughput.
# Enable GC tracing
GODEBUG=gctrace=1 ./myapp
# Set GC target percentage
GOGC=100 ./myapp
# Set memory limit
GOMEMLIMIT=1GiB ./myapp
# Disable GC (dangerous)
GOGC=off ./myapp
# View GC stats programmatically
go tool pprof http://localhost:6060/debug/pprof/heap?gc=1GODEBUG=gctrace=1 prints GC timing and heap statistics. GOGC controls the GC target. GOMEMLIMIT sets a soft memory limit. These are the primary knobs for GC tuning.
GOGC Tuning
GOGC controls the tradeoff between memory usage and CPU overhead. It is the percentage of live heap that triggers a GC cycle. The default is 100 — GC triggers when the heap doubles.
GOMEMLIMIT
GOMEMLIMIT sets a soft memory limit. The GC becomes more aggressive as the limit approaches. This prevents OOM kills in containers with fixed memory limits.
Heap Goals
The GC sets heap goals based on GOGC and live heap size. Understanding these goals helps predict memory usage and GC behavior.
GC Pacer
The GC pacer coordinates the concurrent GC with application goroutines. It adjusts the assist ratio — how much GC work each goroutine must do — to meet the heap goal.
Monitoring GC
For containerized services, start by setting GOMEMLIMIT to 80% of the container memory limit. This prevents OOM kills without tuning GOGC. Only tune GOGC if you need to optimize the memory-CPU tradeoff further.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.