How to Use GOMEMLIMIT (Go 1.19+)

Set GOMEMLIMIT via environment variable or runtime/debug.SetMemoryLimit to cap Go application memory usage.

Set the GOMEMLIMIT environment variable to a byte value with an optional unit suffix (B, KiB, MiB, GiB, TiB) to enforce a soft memory limit on the Go runtime. The runtime will attempt to keep total memory usage below this threshold by adjusting garbage collection behavior.

export GOMEMLIMIT=512MiB

Alternatively, set the limit programmatically at runtime using runtime/debug.SetMemoryLimit:

import "runtime/debug"

func main() {
    debug.SetMemoryLimit(512 * 1024 * 1024)
}