Set GOMAXPROCS for small Go programs on big hosts

Multi-tenant hosts often run many copies of small Go programs: shims, credential refreshers, sidecars, one per container or one per tenant. Each copy does little work, but each one starts with GOMAXPROCS set to the number of CPUs it can see, which on a large host is the whole machine. The Go runtime sizes parts of its allocator, its garbage collector, and its thread pool by GOMAXPROCS, so every copy carries overhead for 64 processors on a 64-vCPU host.

On a 64-core host, 100 copies of a small polling program used 684 MiB of anonymous memory, 2,240 OS threads, and 0.98 CPU cores with the default GOMAXPROCS=64. With GOMAXPROCS=2 the same 100 copies used 503 MiB, 476 threads, and 0.54 cores. This post shows where the difference comes from, when Go already picks a small value for you, and what the same programs cost in Rust.

What GOMAXPROCS sizes besides the scheduler

GOMAXPROCS is the number of Ps, the scheduler's logical processors. A goroutine needs a P to run Go code, and an OS thread (an M) needs a P to run a goroutine. The P is also where the runtime keeps per-processor state so that the hot paths need no locks:

  • Each P owns an mcache, which holds one span (a run of pages carved into objects of one size class) for every size class that P has allocated from. A span cached by one P cannot serve allocations on another P, so the same live data spreads over more partly filled pages when more Ps allocate.
  • The P struct itself is allocated for every P when the program starts, whether or not it ever runs a goroutine. It holds the local run queue, caches of spans, stacks, and timers, and a write barrier buffer.
  • Every P that runs at the same time needs an M. Each M has its own system stack and signal stack, and the runtime keeps idle Ms parked instead of exiting them. The thread count of a Go program is a high-water mark of its parallelism.
  • The garbage collector starts a background mark worker per P and dedicates a quarter of the Ps to marking during a cycle, which is 16 threads at GOMAXPROCS=64.
  • sync.Pool keeps one shard per P. Standard library pools such as the encoder state in encoding/json and the printer in fmt, and any library pool, hold up to GOMAXPROCS sets of cached objects.

None of these change how much live data the program has. They change how much memory and CPU the runtime spends around it.

One copy

The test program is main.go and the driver is run.sh. The program runs in one of three modes:

  • idle does nothing after start-up.
  • burst starts 256 goroutines once, each allocating 2,000 objects between 16 B and 4 KiB, keeps about 1 MiB of them live, runs a GC, and then idles. It stands in for start-up work or an occasional parallel job.
  • ticker runs 8 goroutines that wake every 10 ms and JSON-encode a small record, like a sidecar that polls, formats, and forwards.

Each mode ran three times per GOMAXPROCS value on an otherwise lightly loaded 64-core Intel Xeon 8375C host with Linux 6.12 and Go 1.27.1. The script reads RssAnon and Threads from /proc/PID/status after 5 seconds and CPU time from /proc/PID/stat over the next 10 seconds. Averages are below; the raw rows are in results.txt.

GOMAXPROCS 1 2 4 8 16 32 64
idle, anon RSS (MiB) 0.9 1.0 1.0 1.0 1.2 1.5 2.1
burst, anon RSS (MiB) 4.3 5.2 5.4 4.5 5.5 7.5 12.7
burst, threads 3 4 6 10 18 34 66
ticker, anon RSS (MiB) 2.9 3.1 3.4 3.5 3.7 4.1 4.6
ticker, CPU (ms per second) 3.0 5.3 5.7 6.3 7.3 6.3 7.3

The idle program grows from 1.0 to 2.1 MiB between 2 and 64 Ps without running any code of its own. That is the P structs and their mcaches: the runtime's own counters show heap in use going from 0.4 to 1.3 MiB and mcache metadata from 4 to 143 KiB.

The burst program shows the high-water mark. Its live data is the same at every setting, and two seconds after the burst it is idle again, but at GOMAXPROCS=64 it keeps 66 threads and 12.7 MiB. The runtime counters printed by the program (stats.log) split it like this:

burst, after GC GOMAXPROCS=2 GOMAXPROCS=64
heap in use 1,648 KiB 3,800 KiB
goroutine and thread stacks 640 KiB 3,456 KiB
span metadata 73 KiB 813 KiB
mcache metadata 4 KiB 143 KiB
GC cycles 165 308

The heap grows because the same objects sit in more partly filled spans. The stacks grow because 66 threads each keep a system stack and a signal stack. The GC ran almost twice as many cycles for the same allocation work, because spans cached by 64 Ps count toward the heap goal and the heap reaches it sooner.

The ticker program is the common case for a sidecar: a few goroutines, a little allocation, a lot of sleeping. Per copy the difference is small, 1.5 MiB and 2 ms of CPU per second between 2 and 64 Ps.

One hundred copies

The second half of run.sh starts 100 copies of the ticker program at once and sums their memory, threads, and CPU over 30 seconds.

100 copies of ticker GOMAXPROCS=2 GOMAXPROCS=64
total anon RSS 503 MiB 684 MiB
total threads 476 2,240
total CPU 538 ms/s 982 ms/s
per copy: anon RSS 5.0 MiB 6.8 MiB
per copy: threads 4.8 22.4
per copy: CPU 5.4 ms/s 9.8 ms/s

At GOMAXPROCS=2 a copy costs about the same alone or in a crowd. At GOMAXPROCS=64 it costs more in a crowd than alone: 22 threads instead of 7, and 9.8 ms of CPU per second instead of 7.3. When 100 programs share the CPUs, a thread that should run a woken goroutine is often still waiting for the kernel to schedule it, and with idle Ps available the Go scheduler wakes another thread instead. Each copy ends up using more of its Ps at once, which brings in more threads, more mcaches, and more stacks. The overhead grows with the load from the neighbors, which is the opposite of what a dense host needs.

The extra CPU is mostly scheduler work: threads spinning to look for goroutines, waking and parking, and GC workers spread across more Ps. It does no useful work for the program and competes with the tenants that do.

Does Go pick a small value already?

Since Go 1.25, the default GOMAXPROCS on Linux is the smallest of:

  • the number of CPUs in the process's CPU affinity mask, which is how a cpuset limits it, and
  • the cgroup CPU quota (cpu.max, quota divided by period), rounded up, with a minimum of 2.

The runtime also re-checks these about once a second and adjusts GOMAXPROCS if they change. The logic is in runtime/cgroup_linux.go.

That covers a container with a hard CPU limit, such as docker run --cpus=1 or a Kubernetes pod with a CPU limit. It does not cover a container that is only weighted, with cpu.weight from docker run --cpu-shares or a Kubernetes CPU request without a limit. Many multi-tenant platforms run small utilities in host cgroup without cpu limit, so they can burst into idle CPU, and those programs still see all 64 CPUs. The host in this post had cpu.max set to max, which is why the default was 64.

The new default also depends on the go line in the main module's go.mod, not on the toolchain. A program whose go.mod says go 1.24 or earlier keeps the old behavior when built with Go 1.27, through the GODEBUG setting containermaxprocs=0. To see what a running program chose, log runtime.GOMAXPROCS(0) at start-up.

Setting it

The simplest fix is the environment variable, set by whatever launches the program:

1GOMAXPROCS=2

If you own the program and want the cap to travel with the binary, set it in main. Calling runtime.GOMAXPROCS stops the runtime's periodic updates, so take the smaller of the cap and the current default to keep a lower cgroup limit in effect:

1func main() {
2    if os.Getenv("GOMAXPROCS") == "" {
3        runtime.GOMAXPROCS(min(2, runtime.GOMAXPROCS(0)))
4    }
5    // ...
6}

Checking the environment first lets an operator still override the value.

Setting a CPU quota on the container also works, and Go 1.25 or later then picks the value by itself. A quota is a scheduling decision, though, not a memory one, and platforms often avoid it for small utilities on purpose.

I use 2, not 1. GOMAXPROCS=1 had the smallest numbers in the table, but with one P a goroutine in a CPU-heavy stretch delays every other goroutine, timers included, until it is preempted, and GC marking competes for the same P. Go picks 2 as its own floor for a cgroup limit, and 2 keeps nearly all of the savings.

Or write it in Rust

GOMAXPROCS=2 removes the overhead that scales with processors, but a Go program still carries a garbage-collected heap and a runtime. The GC does not start a cycle until the heap reaches 4 MiB (at the default GOGC=100), so a program that allocates a little garbage settles at a few MiB no matter how little it keeps. A Rust program frees memory when the owner goes out of scope and has no runtime unless it brings one.

rust/src/main.rs runs the same three workloads, with serde_json for the encoding, on three runtimes: plain OS threads, tokio's single-threaded runtime, and tokio's multi-threaded runtime. run-rust.sh measures them the same way as run.sh, next to the Go program, in the same session. Results are in results-rust.txt.

one copy, anon RSS (threads) idle burst ticker
Go, GOMAXPROCS=64 1.6 MiB (5) 12.5 MiB (66) 4.0 MiB (7)
Go, GOMAXPROCS=2 0.7 MiB (4) 3.9 MiB (4) 2.8 MiB (4)
Rust, std threads 0.15 MiB (1) 1.3 MiB (1) 0.24 MiB (9)
Rust, tokio current-thread 0.16 MiB (1) 0.6 MiB (1) 0.16 MiB (1)
Rust, tokio default 1.1 MiB (65) 2.8 MiB (65) 1.1 MiB (65)
100 copies of ticker anon RSS threads CPU
Go, GOMAXPROCS=64 683 MiB 2,238 964 ms/s
Go, GOMAXPROCS=2 502 MiB 458 508 ms/s
Rust, std threads 24 MiB 900 530 ms/s
Rust, tokio current-thread 16 MiB 100 121 ms/s
Rust, tokio with 2 workers 19 MiB 300 218 ms/s
Rust, tokio default 114 MiB 6,500 278 ms/s

Rust on tokio's current-thread runtime used 16 MiB for 100 copies, against 502 MiB for Go at GOMAXPROCS=2, and a quarter of the CPU. With plain threads, memory stays as low, but CPU matches Go: each of the 8 loops has its own thread for the kernel to wake every 10 ms, while one tokio thread serves all eight timers.

Rust does not escape the per-core default. tokio's multi-threaded runtime, which is what #[tokio::main] builds, starts one worker thread per CPU. An idle copy had 65 threads, and 100 copies had 6,500. TOKIO_WORKER_THREADS=2 is the tokio equivalent of GOMAXPROCS=2, and for a small utility the current-thread runtime, #[tokio::main(flavor = "current_thread")], is the better default.

The release binary is also smaller, 0.9 MB against 4.0 MB for Go, though file-backed pages of one binary are shared between its copies and matter less than anonymous memory.

For a new utility that will run in hundreds of copies per host, I would write it in Rust on a current-thread runtime. The savings per copy are a few MiB of memory and a few milliseconds of CPU per second, which is nothing for one program and is the whole budget when the program is multiplied by the number of tenants. For an existing Go utility, try GOMAXPROCS=2.

I'm currently open to engineer opportunities. If you liked this post and would like to have me on your team, reach out on X, LinkedIn or GitHub.