Introduction: Escape Analysis is a technical point that is not used in many JVM technologies. This article will analyze its usage scenarios through an example.
Escape analysis is a cross-functional global data flow analysis algorithm that can effectively reduce the synchronous load and memory heap allocation pressure in Java programs. Through escape analysis, the Java Hotspot compiler can analyze the scope of use of a new object's reference and determine whether to allocate the object to the heap.
In the optimization principle of computer language compiler, escape analysis refers to the method of analyzing the dynamic range of pointers, which is associated with pointer analysis and appearance analysis of compiler optimization principle. When a variable (or object) is allocated in a method, its pointer may be returned or referenced globally, which will be referenced by other processes or threads. This phenomenon is called the escape of pointer (or reference).
Java supports and enables escape analysis options in Java SE 6u23 and later versions. Java's HotSpot JIT compiler can perform escape analysis on the code when the method is overloaded or dynamically loaded. At the same time, the characteristics of Java objects allocating on the heap and built-in threads make escape analysis an important function of Java.
The above paragraph is a passage I quoted from someone else. The article uses a lot of professional terms. Let me summarize its meaning:
Escape analysis is used to determine whether certain instances or variables are allocated in the heap. If escape analysis is enabled, these variables can be allocated directly on the stack rather than on the heap. Pointers to these variables can be referenced globally, or referenced by other threads.
Turn on settings
The default is enabled on JDK 6u23 or above. Here we will re-defined the settings:
Forced to open
-server -XX:+DoEscapeAnalysis -XX:+PrintGCDetail -Xmx10m -Xms10m
Close escape analysis
-server -XX:-DoEscapeAnalysis -XX:+PrintGCDetail -Xmx10m -Xms10m
Instance Verification
Code:
public class OnStackTest { public static void alloc() { byte[] b = new byte[2]; b[0] = 1; } public static void main(String[] args) { long b = System.currentTimeMillis(); for (int i = 0; i < 100000000; i++) { alloc(); } long e = System.currentTimeMillis(); System.out.println(e - b); } }Turn on escape operation results:
Write a picture description here
Running results of escape analysis not enabled:
Write a picture description here
To analyze, here is to allocate 2 bytes of data cycles of 10 million times, the running time for activate escape is 8 milisecond, while the unactivated is 956, which is nearly 1/120 of unactivated.
The difference effect is still very obvious...
The space on the stack is generally very small, and can only store several changes and small data structures, and large-capacity storage structures cannot be achieved. The example here is an extreme tens of millions of times, highlighting the escape analysis, allowing it to be allocated directly from the stack, which greatly reduces the number of GCs and improves the overall execution efficiency of the program.
Therefore, the effect of escape analysis can only take effect in specific scenarios, which meets the variable allocation structure with relatively small capacity with high frequency and high number.