How to Monitor GC Performance in Go

Monitor Go GC performance by setting GODEBUG=gctrace=1 or using the runtime/metrics package to track pause times and heap usage.

You monitor Go garbage collector (GC) performance by enabling the GODEBUG environment variable to print GC statistics to stderr. Set GODEBUG=gctrace=1 before running your application to see real-time metrics like pause times, heap usage, and GC cycles.

export GODEBUG=gctrace=1
go run main.go

Alternatively, use the runtime/metrics package in your code to programmatically access GC metrics like cgo/go/gc/cycles or cgo/go/gc/pauses without relying on environment variables.