Introduction: Stack allocation and escape analysis is the JVM level of Java performance optimization of a skill, this article will be in-depth interpretation of its application and principles.
1. What is allocated on the stack.
On-stack allocation is primarily defined as the variables declared in the method body and the objects created in the execution of the Java program, which allocates space directly from the stack used by the thread. In general, the creation object is allocated from the heap, which means allocating space on the stack to the newly created object.
2. What is escape.
Escape is an object created within a method, in addition to being referenced within the method body, it is referenced outside the method body by other variables; The consequence is that the object created in the method cannot be reclaimed by GC after the method has finished executing, because it is referenced by other variables. In a normal method call, the object created in the method body will be reclaimed after execution, and the object created therein will be recycled, so it becomes escape because it cannot be recycled.
Escape in several situations:
Static V Global_v;
public void A_method () {
V v=b_method ();
C_method ();
}
Public v B_method () {
v v=new v ();
return v;
}
public void C_method () {
global_v=new V ();
}
Where the reference to the V object generated inside the B_method method is returned to the variable V,c_method method within the A_method method, the resulting V object is assigned to the global variable Global_v. Both of these scenarios have occurred (citing) escape.
3. Escape analysis
After JDK 6, the stack analysis and escape analysis of the supported objects is fully supported on the stack in JDK 7. Whether or not the escape analysis is turned on depends on the settings of the following JVM:
-xx:+doescapeanalysis
4. The relationship between stack allocation and escape analysis
After the escape analysis, the result is that all objects will be allocated from the stack, not from the heap in the JVM's memory model.
5. Escape analysis/Stack distribution on the pros and cons of analysis
The advantages are shown in the following two aspects:
Eliminates 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 accessible only by one thread, and if it is accessed by only one thread, then the synchronization of the object can be converted to an operation without synchronous protection, which can greatly increase the degree of concurrency and performance. Vector substitution. Escape Analysis Method If you find that the memory structure of an object does not need to be continuous, you can save a portion of the object or even all of it in a CPU register, which can greatly improve the access speed. Disadvantage: Stack allocation is limited by the size of the stack, the general self-iterative class requirements and large object space requirements operations, will lead to memory overflow stack, so only applicable to a certain range of memory range request. 6. Code Sample analysis Source code is as follows:
Package ORG.EDS.HOMEWORK.JVM;
public class Stackontest {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);
}
To set parameters for the JVM:
-server-xmx10m-xms10m-xx:+doescapeanalysis-xx:+printgc
The settings for eclipse are as follows:
The results of the execution are as follows: No Escape analysis settings:-SERVER-XMX10M-XMS10M-XX:-DOESCAPEANALYSIS-XX:+PRINTGC
The results of the implementation are as follows: The results of the program analysis: in the running results of the escape analysis, only 9 times to exit the program. The result of no escape analysis is 1360 times, which means that code that does not perform escape analysis can perform more calls. In other words, is not the escape analysis of the heap space is much larger than the escape analysis used after the stack space, the heap space is larger than the stack, which is the root cause. 7. Summary stack allocation can improve code performance, reduce the use of locks in multi-threaded situations, but will be limited by the size of their space. 8. Reference http://blog.csdn.net/ykdsg/article/details/6255618 http://www.cnblogs.com/gaiwen/archive/2013/09/24/ 3337835.html