To cross the Yellow River ice jam, will climb the Taihang snow full mountain, difficulties have always been, resistance has always existed, can do things what is the original intention, is to cross the Yellow River and climb the Taihang ah, and if not ice jam, and snow full mountain, perhaps we do things are not so meaningful.

Let’s talk about JVM escape analysis

What is escape? Take a look at the official statement:

If a subroutine allocates an object and returns a pointer to that object, the place in the program where the object was accessed may not be determined-thus the pointer has successfully “escaped.” Pointers also escape if they are stored in global variables or other data structures, because global variables can be accessed outside the current subroutine

JVM determines escape:

The JVM determines whether a newly created object is escaping by: 1. The object is assigned to the field and class static variables of the object in the heap. 2. Objects are passed into indeterminate code to run. If any of these conditions are met, the object is judged to have escaped by the JVM.

Java compiled files come in two forms, static and runtime. Static files are not yet run. If you click “compile”, Java will compile Java files into class bytecode files using JDK. During JIT compilation, the JVM needs to know what all the objects in the program are doing and where they are being used for GC processing and so on.

The JVM does not know where the object is referenced, because the object is stored in the heap. We know that the GC looks for references to determine whether the object is used or not

So that’s why escape analysis is so important. Let’s look at a little bit of code

public class EscapeTest {

    public static Object globalVariableObject;

    public Object instanceObject;

    public void globalVariableEscape(){
        globalVariableObject = new Object(a);// Static variable, external thread visible, escape}

    public void instanceObjectEscape(){
        instanceObject = new Object(a);// Assign to the instance field in the heap, visible to external threads, escape occurs}
    
    public Object returnObjectEscape(){
        return new Object(a);// return instance, external thread visible, escape occurs}

    public void noEscape(){
        synchronized (new Object()) {// create thread only visible, object no escape}
        Object noEscape = new Object(a);// Create thread only visible, object no escape}}
Copy the code

Without enabling escape analysis, then these all escape.

So many escapes, that’s not gg? Don’t worry, escape analysis is turned on by default by the JVM

Initial escape analysis

-server -XX:+DoEscapeAnalysis -XX:+PrintGCDetail -Xmx10m -Xms10m

Shut-off escape analysis

-server -XX:+DoEscapeAnalysis -XX:+PrintGCDetail -Xmx10m -Xms10m

I wonder what he can do

1 can put objects on the stack

Methods put objects on the stack while they’re running, pop the stack when they’re done, and that’s ok, and it reduces GC, and it breaks the stereotype that all objects are allocated in the heap.

2 synchronization lock optimization

If a method that holds a synchronous lock is called by only one thread then the JIT will optimize the lock away

Before optimization

public synchronized void invoke(){
    Object obj = new Object(a); }Copy the code

The optimized

publicvoid invoke(){
    Object obj = new Object(a); }Copy the code

A class file is fixed. Jit compilation and static compilation should be the same. Why should escape analysis be jit specific

3 Analyze object and variable replacement

The explanation for this is to program an object to its primitive type, and then assign it to the heap, which is also a stack assignment