Every time your program calls new Point(3, 4), most runtimes reach for the heap: a shared pool of memory managed by the garbage collector. Heap allocation is flexible â objects can outlive the function that created them â but it comes with a cost: the GC must eventually find and reclaim each dead object, causing pauses and cache misses.
A large fraction of real allocations, however, are local by nature: a temporary result, a buffer used inside one loop, a pair of coordinates that never leaves the method. These objects are created and discarded all within a single call frame. Putting them on the heap is wasteful â the stack could hold them for free, releasing them the moment the function returns.
Escape analysis is the static technique a compiler uses to prove this. It tracks where each object reference flows: if the reference never leaves the creating scope â never gets stored in a global, never gets returned, never crosses a thread boundary â then the object does not escape, and the compiler can allocate it on the stack instead of the heap.
The name comes from the key question: does this object escape its birthplace?
Comments
Loading comments...