Java escape analysis

What is the escape analysis?

About Java escape analysis definition:

Escape analysis (Escape Analysis) simple terms is, Java Hotspot virtual machine can analyze the use of the newly created object, and decide whether a heap memory allocation techniques in Java.

JVM escape analysis of the following parameters:

  • Open escape analysis: -XX: + DoEscapeAnalysis

  • Close escape analysis: -XX: -DoEscapeAnalysis

  • The results show: -XX: + PrintEscapeAnalysis

Escape analysis techniques 6u23 + began to support the Java SE, and the default setting is enabled, you can not add this additional parameter.

Escape analysis algorithm

Java Hotspot compiler implements the following escape algorithm described in the paper:

[Choi99] Jong-Deok Choi, Manish Gupta, Mauricio Seffano,
          Vugranam C. Sreedhar, Sam Midkiff,
          "Escape Analysis for Java", Procedings of ACM SIGPLAN
          OOPSLA  Conference, November 1, 1999

According to Jong-Deok Choi, Manish Gupta, Mauricio Seffano, Vugranam C. Sreedhar, Sam Midkiff and other large cattle in the paper " Escape the Analysis for the Java conduct escape analysis algorithm described in the" middle.

The algorithm introduces FIG communication, communication using FIG constructed reachability relationships between objects and object references, and based on the times, to provide a combined data stream analysis method.

Since the algorithm is context-sensitive and flow, and any level of simulated objects in a nesting relationship, so high precision analysis, just run time and memory consumption is relatively large.

Object escape state

We understand the escape analysis techniques in Java, come to know the next object in the escape state.

1, the overall escape (GlobalEscape)

That scope of an object to escape the current method, or the current thread, are the following scenario:

  • The object is a static variable

  • The object is an object that has occurred escape

  • Object as the return value of the current

2, slip parameter (ArgEscape)

I.e., an object is or is passed as a parameter reference parameters, but does not occur during the call overall escape, this state is established by the called method bytecode determined.

3, there is no escape

That method does not occur in the target escape.

Escape analysis and optimization

Above for the third point, when an object is no escape, the following can be optimized virtual machine.

1) Lock elimination

We know the thread synchronization lock is sacrificing performance, when the compiler determines the current object using only the current thread, it will remove the synchronization lock the object.

For example, StringBuffer and Vector are synchronized with modified thread-safe, but in most cases, they are only used in the current thread, so the compiler will optimize these away to remove the lock operation.

Lock elimination of JVM parameters are as follows:

  • Open locks elimination: -XX: + EliminateLocks

  • Close locks elimination: -XX: -EliminateLocks

In JDK8 eliminate lock is turned on by default, and eliminate lock to be built on the basis of the analysis of the escape.

2) scalar replacement

First, to understand and aggregate scalar quantity, type, and object reference base can be understood as a scalar, they can not be further decomposed. And the amount of the polymerization can be further decomposed amount is, for example: an object.

The amount of the polymerization is the object, it may be further broken down into scalar, which is divided into dispersion member variables variable, which is called scalar replacement.

Thus, if an object escape did not happen, it absolutely does not create it, it will create a scalar component used in the register or on the stack, saving memory space, but also enhance application performance.

Alternatively JVM scalar parameters are as follows:

  • Open scalar replacement: -XX: + EliminateAllocations

  • Close scalar replacement: -XX: -EliminateAllocations

  • Display scalar replacement Details: -XX: + PrintEliminateAllocations

Scalar replacement in the same JDK8 are enabled by default, and must be based on the analysis of the escape.

3) allocated on the stack

When the object to escape does not occur, the object can be scalar replacement decomposed into members scalar allocated on the stack memory, and lifecycle methods consistent destroyed when as the stack frame from the stack, reducing the GC pressure, improve application performance .

to sum up

Escape analysis finished, summed up a lot of time, we should also probably know escape analysis it is to optimize the JVM memory and improves program performance.

We know this, in the normal development process must be controlled as much as possible the scope of the variable, variable range as small as possible, so that the virtual machine as there is room for optimization.

For a simple example of it, such as:

return sb;

Can be changed to:

return sb.toString();

This is an optimization case, the StringBuilder variable controls within the current method, the method does not escape the current scope.

Do you have any other optimization experience, please share ~

References:

  • https://docs.oracle.com/javase/8/docs/technotes/guides/vm/performance-enhancements-7.html#escapeAnalysis

  • https://blog.csdn.net/rickiyeat/article/details/76802085

  • https://blog.csdn.net/baichoufei90/article/details/85180478

Guess you like

Origin blog.csdn.net/Dome_/article/details/92080804