1. Escape analysis

Escape analysis is an analysis technique provided for GC optimization of the JVM to determine the scope of a variable;

  • Methods the escape

    The scope of a variable is not limited to the current method;

  • Thread escape

    The scope of a variable is not limited to the current thread; Thread escape currently cannot be optimized;

2. Stack allocation

  • The created object allocates memory on the stack, not on the heap, and when the method is finished, the occupied memory is reclaimed as the stack frame pops up;
  • Allocation on the stack can only optimize method escape, not thread escape.
  • -xx :+DoEscapeAnalysis Enables escape analysis (enabled by default in JDK8), -xx: -doescapeAnalysis disables escape analysis;
  • Example:

3. Scalar substitution

  • Scalar, can be understood as atomic, is no longer able to decompose, in Java the basic data type is scalar; The opposite of a scalar is an aggregate quantity, such as an object;

  • Scalar replacement is to replace the member variables in the object with scalar according to the access condition, and put them into the local variable table of stack frame. Instead of actually creating the object;

  • Parameter -xx :+EliminateAllocations turns on scalar substitutions (JDK8 is on by default);

  • Example (pseudocode)

  • // After escape analysis, user does not escape
    public static int escapeTest(a){
      int age = 20;
      User user = new User();
      user.setAge(age);
      user.setName("23");
      return user.getAge();
    }
    
    // inline optimization
    public static int escapeTest(a){
      int age = 20;
      User user = new User();
      user.age = age
      user.name = "23";
      return user.age;
    }
    
    // If scalar substitution is enabled, scalar substitution is used
    public static int escapeTest(a){
      int age = 20;
      int userAge = age
      int userName = "23";
      return userAge;
    }
    
    // Find that userAge and userName have no effect on the method, so we can remove them
    public static int escapeTest(a){
      int age = 20;
      return age;
    }
    Copy the code

4, synchronous elimination

  • If escape analysis can determine that a variable will not be thread escaped, then there will be no contention for the read and write of the variable. Therefore, the synchronization lock of the variable can be canceled. Synchronization itself is a time-consuming operation, which can be eliminated to improve performance.
  • Essentially, a synchronous lock is changed to a biased lock;
  • Parameter -xx :+EliminateLocks Enable EliminateLocks synchronization (JDK8 is enabled by default);
  • The instance

Common parameters:

-XX:DoEscapeAnalysis // Whether escape analysis is enabled (whether stack allocation is allowed) + : enabled, - : disabled
-XX:EliminateAllocations // Whether scalar replacement is enabled (on the basis of enabling escape analysis) + : enabled, - : disabled
-XX:EliminateLocks // Whether to enable sync elimination (on the basis of enabling escape analysis) + : enabled, - : disabled
Copy the code