Memory Fragmentation in Long-Lived Go Services: GOGC and GOMEMLIMIT in Practice

· Daniel Lin golangperformancememory

One of our Go edge aggregators ran smoothly for weeks until traffic doubled during a promotional event, at which point Kubernetes began OOMKilling pods even though heap profiles showed only 60% live object utilization.

Understanding MADV_FREE vs. Container RSS Accounting

Linux cgroup v2 tracks anonymous memory aggressively. Under sudden allocation spikes, Go's default GOGC=100 lets the heap double before triggering a GC cycle, easily breaching tight container memory limits before the runtime can scavenge idle spans back to the OS.

The Fix That Stabilized RSS

// Avoid pooling abnormally large buffers
func putBuf(b *bytes.Buffer) {
    if b.Cap() > 64*1024 {
        return
    }
    b.Reset()
    bufPool.Put(b)
}
← Why We Replaced Complex Service Meshes with Boring L4 Load BalancersTuning Linux TCP BBR and Fair Queueing on Cloud Virtual NICs →