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
GOGC tells Go how aggressively to clean up unused memory, while GOMEMLIMIT sets a strict maximum on how much memory your program can use. Think of GOGC as adjusting how often you take out the trash, and GOMEMLIMIT as the size of the trash can itself.