Tobias Schottdorf    About    Archive    Feed

Stack vs Heap: Improving memory allocation through benchmarks

Recently petermattis from cockroach labs shared a simple method for finding weak spots in your Go project’s memory management:

I found this using the memory profiler: go test -bench=. -memprofile=prof -memprofilerate=1. The -memprofilerate=1 setting tells the memory profiler to record every allocation. This slows the benchmark down a lot, but gives a price view of where the allocations are occurring. I then use go tool pprof --alloc_objects <binary> prof to view the output.

If you’ve found such a spot in which the escape analysis doesn’t want to put things on the stack (for example, because it’s a rather large byte slice), you could use a sync.Pool to avoid an allocation each time the problematic code gets called, or maybe you have your concurrent access under control - then a scratch buffer is enough.