Escape analysis is an analytical technique that provides the basis for other optimization methods, and its basic behavior is to analyze the object's dynamic scope: When an object is defined in a method, it may be referenced by an external method, such as a call parameter passed to another method, called a method escape. It is even possible to be accessed by external threads, such as assignment to class variables or instance variables that can be accessed in other threads, called thread escapes.
If you can prove that an object does not escape to a method or thread, that is, other methods or threads can not access the object in any way, it is possible for this variable to do some optimization of the university.
1) on the stack allocation, if you determine that an object does not escape out of the method, then the object is allocated on the stack memory, the object occupied by the memory space can be destroyed with the stack frame stack. In general applications, the proportion of local objects that do not escape is very large, and if you can use the allocation on the stack, then the object will be automatically destroyed as the method ends, and the pressure on the garbage collection system will be much smaller.
2) Synchronous elimination, thread synchronization itself is a relatively time-consuming process, if the escape analysis can determine that a variable does not escape the thread, can not be accessed by other threads, then this variable read and write certainly will not have competition, the implementation of this variable can be eliminated.
3) scalar substitution: If the escape analysis proves that an object is not being accessed externally. And this object can be torn apart, the program actually executes the time may not create this object, and instead of directly create a number of it is used by the method of the member variable to replace.
Escape analysis of Java Virtual machine