JVM principle | escape analysis

Escape analysis

1. What is "Escape"?

When a variable (or object) is allocated in a method, its pointer may be returned or referenced globally , so that it will be referenced by other methods or threads. This phenomenon is called pointer (or reference) escape.

Example:

public StringBuilder escapeDemo1(String a, String b) {
    
    
	StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.append(a);
      stringBuilder.append(b);
      return stringBuilder;
}

The above code will escape


If it is changed to:

public String escapeDemo2(String a, String b) {
    
    
     StringBuilder stringBuilder = new StringBuilder();
     stringBuilder.append(a);
     stringBuilder.append(b);
     return stringBuilder.toString();
}

No escape

2. What is escape analysis

Escape analysis is a cross-function global data flow analysis algorithm that can effectively reduce synchronization load and memory heap allocation pressure in Java programs. Through escape analysis, the Java Hotspot compiler can analyze the use range of a new object reference to determine whether to allocate this object to the heap .
逃逸分析(Escape Analysis)算是目前Java虚拟机中比较前沿的优化技术了。

3. Principles of escape analysis

The limitation of Java itself (objects can only be allocated to the heap), in order to reduce the number of temporary objects allocated in the heap, I will define a local variable in a method body, and the variable does not escape during the execution of the method, according to JVM Tuning mechanism,

  1. Before optimization: First, an instance of the class will be created in the heap memory, and then the reference of this object will be pushed onto the call stack to continue execution
  2. After optimization: For the stack re-allocation method, first find out the variable that has not escaped, and save the variable directly to the stack without entering the heap. After the allocation is completed, the call continues to execute in the stack, and finally the thread execution ends, and the stack space is reclaimed , Local variables are also recycled. This operation is in the heap before optimization and in the stack after optimization, thereby reducing the allocation and destruction of objects in the heap, thereby optimizing performance.

Summary: Objects that do not escape only exist in the stack, which reduces the heap pressure and thus obtains the optimization effect;

4. Ways to escape

  1. Method escape: In the body of a method, a local variable is defined, and it may be referenced by an external method, such as being passed to the method as a call parameter, or directly returned as an object. Or, it can be understood that the object has jumped out of the method.
  2. Thread escape: This object is accessed by other threads, such as assigned to an instance variable, and accessed by other threads. The object escaped from the current thread.

5. Benefits of escape analysis

If an object will not be in the method body, or escape in the thread (or after escape analysis, make it fail to escape)

Three advantages after escape analysis:

  1. Allocation on the stack : Under normal circumstances, objects that will not escape occupies a relatively large space. If the space on the stack can be used, a large number of objects will be destroyed with the end of the method, reducing the GC pressure

  2. Synchronization elimination : If there is a synchronization lock on the method of the class you define, but only one thread is accessing it at runtime, the machine code after escaping analysis will run without the synchronization lock.

  3. Scalar substitution :

    (int,long等数值类型以及reference类型等)None of the primitive data types in the Java virtual machine can be further decomposed, they can be called scalars . In contrast, if a piece of data can continue to be decomposed, it is called an aggregate amount . The most typical aggregate amount in Java is an object.

    If the escape analysis proves that an object cannot be accessed externally, and the object is decomposable, the object may not be created when the program is actually executed, and several of its member variables used by this method may be directly created instead. Instead . The disassembled variables can be analyzed and optimized separately, and 各自分别space can be allocated on the stack frame or register, and the original object does not need to allocate space as a whole.

6. Related JVM parameters

Parameters open:

It is enabled by default in JDK 6u23 and above. Here, the settings will be clarified again:

​ Forced to open:-server -XX:+DoEscapeAnalysis -XX:+PrintGCDetail -Xmx10m -Xms10m

​ Close escape analysis:  -server -XX:-DoEscapeAnalysis -XX:+PrintGCDetail -Xmx10m -Xms10m

Guess you like

Origin blog.csdn.net/weixin_40597409/article/details/115260954