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)
}