What this thread does
Do first: compare the live-byte and total-allocation heap views with go tool pprof before changing a GC setting.
As of November 2026, this recipe targets Go 1.27.3; reference date: 2026-11-08. Run these against an existing private net/http/pprof listener while holding traffic and data size steady. The Go 1.27.3 HTTP documentation describes adding that listener if needed; keep it accessible only through loopback or an authenticated tunnel.
go tool pprof -inuse_space 'http://127.0.0.1:6060/debug/pprof/heap?gc=1'
go tool pprof -alloc_space 'http://127.0.0.1:6060/debug/pprof/heap?gc=1'
These are successive snapshots, so use a stable workload. gc=1 requests a collection before each capture and changes timing. At each prompt, use top and list YourFunction, substituting a function from the report (official walkthrough). The runtime definitions distinguish live allocated bytes from all bytes allocated since startup. A large lifetime allocation total is expected in a long-running process; compare equal work, not raw totals from processes of different ages.
Choose branch 1 when the post-GC live heap keeps growing, branch 2 when live heap stabilizes but allocation rate and GC cost are high, or branch 3 when a bounded live heap still leaves too little room under the process memory budget.
1. Live heap grows: release the object that remains reachable
As of November 2026, Go 1.27.3: follow the official slice retention explanation. If the dominant allocation is a large byte buffer and your code stores only a small subslice, the subslice still keeps the backing array reachable. At the ownership boundary, copy the retained portion:
retained := make([]byte, len(part))
copy(retained, part)
Here part is the byte subslice your code already retains; store retained in its place and stop keeping the original buffer or its aliases alive. The copy temporarily allocates more memory, so apply it only where the source shows this retention pattern.
Replay several equal batches, allow collections, and compare post-GC live bytes at the same data cardinality. The goal is a plateau and lower retained bytes at that allocation site, with identical retained contents. A heap profile points to allocation sites; inspect the cache, queue or object fields yourself to find the surviving reference.
2. Live heap is stable: remove allocations from one hot path
As of November 2026, Go 1.27.3: follow the GC guide's allocation and escape-analysis path. Capture a fixed allocation window, then ask the compiler about the package containing the dominant stack:
go tool pprof -alloc_space 'http://127.0.0.1:6060/debug/pprof/allocs?seconds=30'
go build -gcflags=-m=3 ./path/to/package
Replace the package path with your actual package. Treat escape output as an explanation, not a requirement that every allocation disappear. For a hot append loop with a known, bounded result count, start with make([]Result, 0, count), using your real type and count. Avoid reserving capacity from an unbounded request value.
If an existing benchmark exercises this path, run go test -run='^$' -bench=BenchmarkYourPath -benchmem -count=5 ./path/to/package before and after; substitute its actual benchmark name. The Go 1.27.3 flags expose B/op and allocs/op. Otherwise compare allocation-window bytes per completed request. Keep the change only if allocated bytes fall while results and latency remain acceptable.
3. Live data is bounded: budget the runtime, then compare GC cost
As of November 2026, Go 1.27.3: follow runtime/debug.SetMemoryLimit. For an illustrative 1 GiB container whose measured non-Go overhead and bursts fit in the remaining space, try:
GOMEMLIMIT=800MiB GODEBUG=gctrace=1 ./service
Replace ./service and the budget with your deployment values. This is a soft limit on memory managed by the Go runtime, not an RSS cap; binary mappings, C allocations and other external memory need separate headroom. A limit below the live working set can make GC nearly continuous.
Record peak process/container memory, latency and the GC CPU share from the Go 1.27.3 trace output. If GC CPU remains expensive with substantial spare memory, compare a second run using GOGC=200 GOMEMLIMIT=800MiB GODEBUG=gctrace=1 ./service. The GC guide explains the tradeoff: a higher GOGC permits more heap growth between collections, while the memory limit may constrain that target. A lower GOGC can reduce heap headroom at additional GC cost. Neither change removes live objects.
Keep a setting only if the same peak workload fits its memory budget and meets latency requirements. Do not use a single percentage as evidence of improvement when throughput differs.
Evidence and limits
The release history dates Go 1.27.3 to 2026-10-19. Etienne Dilocker's Weaviate experiment, 2022-08-15, compares container imports with and without GOMEMLIMIT on Go 1.19. It demonstrates the budget tradeoff for that workload; its settings and outcomes do not predict your service's savings.
Forcing GC is a diagnostic action, not a per-request fix. Memory samples can omit small or recent allocations. A flat Go heap with rising RSS calls for investigation of non-heap or external memory; lowering GOGC cannot explain or reclaim every byte in RSS. Do not copy historical experimental collector flags into this recipe.
Not for: Node.js heap limits; see node-javascript-heap-out-of-memory-build-or-script-max-old-space-size
Started from a post by @Etienne Dilocker, packaged by the editor. Original post