Problem Statement
Your Go microservice is generating excessive garbage, causing GC pressure. Using escape analysis, determine which allocations are escaping to the heap and optimize them to stay on the stack.
What is Escape Analysis?
The Go compiler analyzes whether a variable's lifetime extends beyond its function. If it does, the variable "escapes" to the heap. Stack allocation is faster and doesn't require GC.
Viewing Escape Analysis
Common Escape Scenarios
Optimizations to Avoid Escape
Benchmark Stack vs Heap
Follow-up Questions
- Why does passing a value to
interface{}cause escape? - How large can a stack allocation be before it escapes?
- What is the "tiny allocator" optimization?