← blog · December 2, 2026

Go Services in Containers: GOMAXPROCS, GOMEMLIMIT, CPU Throttling and Hitting the Memory Limit

For years the Go runtime ignored container limits: it sized threads from host cores and grew the heap blind to the limit. What GOMAXPROCS and GOMEMLIMIT fix, what Go 1.25 changed, and why GOGC=off should not be your default.

Move a Go service into Kubernetes and two symptoms tend to show up. Latency percentiles rise even though CPU usage stays below the limit, and memory creeps upward until one day the container dies with OOMKilled. Both have the same root. For a long time the Go runtime had no idea it was inside a container: it sized its thread pool from the host's core count and let the heap grow according to the garbage collector's own ratio. This post covers the two knobs that fix this, GOMAXPROCS and GOMEMLIMIT, what they actually solve, and where people misuse them.

GOMAXPROCS: how many threads run Go code at once

GOMAXPROCS is the number of OS threads that may execute Go code simultaneously. Historically the default was the number of logical CPUs on the machine. A pod with a CPU limit of 2 on a 64-core node got 64.

The problem comes from how Linux enforces CPU limits: the CFS quota. With a limit of 2, the container may spend 200 ms of CPU time in each 100 ms period. If 64 threads run in parallel, that quota is gone within a few milliseconds of the period starting, and the process is parked for the rest of it. Average utilisation looks fine on the dashboard, but individual requests pick up pauses measured in tens of milliseconds. The garbage collector is parallel too, so the stalls are worst during GC cycles.

You can confirm it from cgroup statistics. Inside a cgroup v2 container:

cat /sys/fs/cgroup/cpu.max
cat /sys/fs/cgroup/cpu.stat

The first file shows quota and period. If nr_throttled and throttled_usec in the second keep growing, the process is hitting its quota. With Prometheus, cAdvisor's container_cpu_cfs_throttled_periods_total carries the same signal.

What changed in Go 1.25

Starting with Go 1.25, the runtime on Linux reads the cgroup CPU limit and, when it is lower than the machine's CPU count, sets GOMAXPROCS accordingly. Fractional limits are rounded up, and the runtime periodically re-checks the limit so it can follow changes while the process runs.

Two details matter. First, the behaviour is tied to the go line in go.mod. Upgrade the toolchain but leave an old version in go.mod and the old default may stay in effect for compatibility. These behaviours are governed by GODEBUG settings (containermaxprocs, updatemaxprocs), so check the release notes against your go.mod to know which one is active. Second, the runtime only looks at the CPU limit, never the request. A pod with a request and no limit still gets the node's full core count.

On older Go versions you have two options: set the environment variable explicitly, or blank-import the widely used go.uber.org/automaxprocs package:

import _ "go.uber.org/automaxprocs"

For the environment variable route, Kubernetes can feed it from the limit through the Downward API:

env:
  - name: GOMAXPROCS
    valueFrom:
      resourceFieldRef:
        resource: limits.cpu
        divisor: "1"

With divisor: "1", a fractional limit like 500m is rounded up to 1. If no limit is set, this field reports the node's allocatable CPU, so you are back to a large number.

On 1.25 and later, do not also set the variable. An explicit GOMAXPROCS disables the automatic behaviour, and the value goes stale when the limit changes.

GOMEMLIMIT: giving the collector a ceiling

By default the Go GC runs with GOGC=100: the heap may grow by as much as the live heap before the next collection. A service holding 600 MB of live data can reach 1.2 GB before collecting. With a 1 GB container limit, the kernel kills it. The collector cannot decide to collect earlier because it does not know a limit exists.

GOMEMLIMIT, added in Go 1.19, provides that ceiling. It is a soft limit: the runtime collects more often to keep total memory under the value, but it does not guarantee it will never be exceeded. The value takes a unit suffix:

env:
  - name: GOMEMLIMIT
    value: "900MiB"

It can also be set from code with runtime/debug.SetMemoryLimit.

Picking the value

Do not hand it the whole container limit. GOMEMLIMIT only accounts for memory managed by the Go runtime: the heap, goroutine stacks and runtime structures. Memory allocated by C libraries through cgo, files mapped with mmap, and other processes in the container are outside it. For a pure Go service, 85 to 90 percent of the container limit is a reasonable starting point. If the image uses cgo or runs sidecar processes, leave more headroom and decide from measured usage.

The Kubernetes side matters as well. Keeping the memory request equal to the limit stops the service from losing node memory to its neighbours. Tune GOMEMLIMIT to the limit while leaving the request low, and your pod may be the first one evicted when the node runs short.

Combining it with GOGC=off

Another pattern is GOGC=off together with GOMEMLIMIT. The collector then runs only as memory approaches the ceiling. For services with a small, stable live set and a high allocation rate, the CPU savings can be significant.

I would not make it the default. When the live heap approaches the ceiling, the service collects almost continuously. The runtime caps the CPU share the GC may take to limit this death spiral, but the result is still high latency and eventually an out-of-memory kill. Leaving GOGC at its default and adding only GOMEMLIMIT is safer: behaviour under normal load is unchanged, and collection only speeds up near the ceiling.

Pitfalls

Hiding a leak. GOMEMLIMIT does not fix a memory leak, it delays the symptom. A leaking service stops dying with OOMKilled and instead slows down under rising GC CPU. That is a worse failure mode: instead of a pod that crashes fast and restarts, you get one that is slow and still looks healthy. Watch go_gc_duration_seconds and the runtime's GC metrics.

Confusing RSS with heap. Go does not return freed memory to the OS immediately. The container's memory metric can look high while the live heap is small. Before diagnosing, look at the real picture with the runtime/metrics package or a pprof heap profile.

Old kernels, cgroup v1. Automatic detection depends on reading the cgroup interface. On older nodes or unusual runtimes, log the value at startup to confirm detection did what you expect. runtime.GOMAXPROCS(0) returns the current value without changing it.

Lowering GOMAXPROCS is not always good. For an I/O-heavy service that spends a lot of time in syscalls, a value of 1 can feel like being pinned to a single thread. The fix there is not to lower GOMAXPROCS artificially, but to set a CPU limit that matches real demand.

No CPU limit at all. Some teams remove CPU limits entirely to escape throttling. That can be a valid choice, but then your Go service sees every core on the node as its thread budget and is a candidate noisy neighbour. If you skip limits, pin GOMAXPROCS yourself to something close to the CPU request.

When not to bother

For short-lived CLI tools, batch jobs, or a service that owns the whole machine, these settings buy little. The defaults are already right for the machine. Measure throttling and the memory curve before tuning; without measurements you will not know whether the change helped.

The rule is simple: on Go 1.25 or later keep go.mod current and set a CPU limit, set GOMEMLIMIT a little below the container limit, and try GOGC=off only on services you have measured.