Skip to content
Preptima
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

Further reading