hardConceptCodingScenarioMidSeniorStaff
How do you tell whether a value escapes to the heap, and how would you find the allocations that are costing you?
6 min readUpdated 2026-08-15
Answer
Short answer
Escape analysis moves a value to the heap when the compiler cannot prove its lifetime ends with the frame that created it, and go build -gcflags=-m prints every decision.
Was this answer helpful?
Likely follow-ups
- Why does passing a value to fmt.Println move it to the heap when passing it to your own function does not?
- When does sync.Pool make things worse?
- You reduced allocs/op by half and p99 latency did not move. What do you conclude?
- How would you diff two heap profiles taken an hour apart on a running service?
Related questions
- Memory keeps climbing in a Go service under load. How do you find out why, and what can you actually tune?hardAlso on garbage-collection and pprof6 min
- A long-running Python service keeps climbing in memory and never comes back down. How do you work out where the memory went?hardAlso on garbage-collection and profiling5 min
- How do goroutines leak, and how would you find a leak in a running service?mediumAlso on pprof5 min
- You put @functools.lru_cache on a method to speed it up, and the service's memory keeps growing. Why, and which fix do you choose?mediumAlso on garbage-collection4 min