How to Tune the Go Garbage Collector (GOGC, GOMEMLIMIT)

Set GOGC to adjust GC frequency or GOMEMLIMIT to cap memory usage in Go applications.

Tune the Go garbage collector by setting the GOGC environment variable to control GC frequency or GOMEMLIMIT to cap total memory usage. Use GOGC to adjust the percentage of heap growth that triggers a GC cycle, and GOMEMLIMIT to enforce a hard memory ceiling.

export GOGC=200
export GOMEMLIMIT=4GiB
go run main.go