Escape analysis in Java programming language
It is generally believed that new objects are allocated on the heap, but this is not entirely correct. Through the analysis of the allocation process of Java objects, we find that objects can be allocated not only on the heap, but also in the stack or TLAB. The technical basis of allocating objects on the stack is escape analysis and scalar substitution. This paper mainly introduces escape analysis.
1. Definition of escape analysis
Escape analysis: it is a cross function global data flow analysis algorithm that can effectively reduce the synchronization load and memory heap allocation pressure in Java programs.
Through escape analysis, the Java hotspot compiler can analyze the use range of the reference of a new object and decide whether to allocate the object to the heap.
Java is supported in Java se 6u23 and later versions, and the escape analysis option is enabled by default. Java's hotspot JIT compiler can perform escape analysis on the code when the method is overloaded or the code is loaded dynamically.
The basic behavior of escape analysis is to analyze the dynamic scope of an object: when an object is defined in a method, it may be referenced by an external method.
Method escape: for example, passed as a call parameter to another method.
Thread escape: it may be accessed by external threads, such as assigned to class variables or instance variables that can be accessed in other threads.
2. Theoretical basis of escape analysis
Escape analysis is performed based on the algorithms described in escape analysis for Java by Jong deok Choi, Manish Gupta, Mauricio seffano, vugranam C. sreedhar, Sam Midkiff, etc.
In this algorithm, connected graph is introduced to construct the reachability relationship between objects and object references, and a combined data flow analysis method is proposed. Because the algorithm is context sensitive and flow sensitive, and simulates the nesting relationship of any level of objects, the analysis accuracy is high, but the running time and memory consumption are relatively large.
Most of the implementation of escape analysis is based on the premise of "closed world": all possible methods have been known before escape analysis, and the actual operation of the program will not change the calling relationship between them. But when a real Java program runs, this assumption does not hold. Many features of Java programs, such as dynamic class loading, calling local functions and reflective program calls, will break the so-called "closed world" Convention.
Processing operations after escape analysis
After escape analysis, three possible escape states of an object can be obtained: globalescape: that is, the reference of an object escapes from a method or thread. For example, the reference of an object is copied to a class variable, or stored in an escaped object, or the reference of this object is returned to the calling method as the return value of the method.
Argescape (parameter level escape): that is, the application of the object is passed to a method during the method call process. This state can be determined by analyzing the binary code of the called method.
Noescape (no escape): an object that can be scalar replaced. The object can not be allocated on the traditional heap.
The compiler can use the results of escape analysis to optimize the program.
The heap allocation object becomes the stack allocation object: if the reference of the object in a method does not escape, the method may be allocated to stack memory rather than heap memory.
Eliminate synchronization: the cost of thread synchronization is quite high, and the consequence of synchronization is to reduce concurrency and performance. Escape analysis can determine whether an object is always accessed by only one thread. If it is accessed by only one thread, the synchronous operation of the object can be transformed into an operation without synchronization protection, which can greatly improve the degree of concurrency and performance.
Vector substitution: if the escape analysis method finds that the memory storage structure of the object does not need to be continuous, it can save part or even all of the object in the CPU register, which can greatly improve the access speed.
summary
The above is the whole content of escape analysis in this paper. I hope it will be helpful to you.