## GOMEMLIMIT Environment Variable: GOMEMLIMIT Programmatically: [runtime/debug.SetMemoryLimit](https://pkg.go.dev/runtime/debug#SetMemoryLimit) This memory limit includes the Go heap and all other memory managed by the runtime, and excludes external memory sources such as mappings of the binary itself, memory managed in other languages, and memory held by the operating system on behalf of the Go program. GOMEMLIMIT is a numeric value in bytes with an optional unit suffix. The supported suffixes include B, KiB, MiB, GiB, and TiB. These suffixes represent quantities of bytes as defined by the IEC 80000-13 standard. ### When to use it? - In containerized environments (Docker, Kubernetes), you need to prevent Go from exceeding container memory limits and getting OOM-killed. - Memory-constrained environments where you want to avoid excessive memory consumption by the Go runtime, or leave more memory available for other applications. - Performance tuning to control memory usage and garbage collection behavior, especially in applications with high memory churn. ### When not to use it? - Applications that require dynamic memory allocation and cannot predict memory usage patterns. ## GOGC Environment Variable: GOGC Programmatically: [runtime/debug.SetGCPercent](https://pkg.go.dev/runtime/debug#SetGCPercent) The GOGC variable sets the initial garbage collection target percentage. A collection is triggered when the ratio of freshly allocated data to live data remaining after the previous collection reaches this percentage. The default is GOGC=100. Setting GOGC=off disables the garbage collector entirely. ### When to use it? Increase GOGC (>100) when: - You have abundant memory and want to reduce GC frequency for better performance. - CPU is more constrained than memory--trading memory for fewer GC pauses. - Batch processing or compute-heavy workloads where GC pauses are costly. Decrease GOGC (<100) when: - Memory is constrained, and you need more aggressive GC cycles. - You want lower memory usage at the cost of more frequent GC cycles. - Running in memory-limited environments. Set GOGC=off when: - Building short-lived programs where GC overhead isn't worth it. - You're managing memory manually or have specific memory patterns. - Testing/debugging scenarios where you want predictable memory behavior.