Home  >  Article  >  Java  >  What is JVM escape? Introduction to the principles of JVM escape analysis

What is JVM escape? Introduction to the principles of JVM escape analysis

不言
不言forward
2018-10-08 14:58:593609browse

What this article brings to you is about JVM escape? The introduction to the principles of JVM escape analysis has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

We all know that objects in Java are allocated on the heap by default. In the call stack, only the pointer of the object is saved. When an object is no longer used, GC needs to be relied upon to traverse the reference tree and reclaim memory. If there are too many objects in the heap, recycling objects and organizing memory will consume time. GC will be under great pressure, which will affect performance. Therefore, in our daily development, memory and time are very precious. How to optimize stack overhead is a relatively important issue.

Here, I talk about JVM optimization from the perspective of escape analysis.

Why "Escape"

In the principle of computer language compiler optimization, escape analysis refers to the method of analyzing the dynamic range of pointers. It is the same as Compiler optimization principles are related to pointer analysis and shape analysis. When a variable (or object) is allocated in a method, its pointer may be returned or referenced globally, which will be referenced by other methods or threads. This phenomenon is called pointer (or reference) escape. In layman's terms, if an object's pointer is referenced by multiple methods or threads, then we call it the escape of the object's pointer (or object).

A blogger on the Internet described escape this way, using a simple and direct code. I think it is quite straightforward and can be used for reference:

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

stringBuilder is an internal variable in the method, and this When it is returned directly, stringBuilder may be changed by methods or parameters elsewhere, so that its scope is not just demo1. Although it is a local variable, it "escaped".

Then, I can change the code:

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

In this way, StringBuilder is not returned, but toString(), then StringBuilder is not directly separated from the method, and no escape occurs.

What is escape analysis

Escape analysis is a cross-function global data flow that can effectively reduce synchronization load and memory heap allocation pressure in Java programs. Analysis algorithm. Through escape analysis, the Java Hotspot compiler can analyze the usage range of a new object's reference and decide whether to allocate this object to the heap. Escape Analysis is currently a relatively cutting-edge optimization technology in Java virtual machines.

The principle of escape analysis

I can understand the limitations of Java itself (objects can only be allocated in the heap), in order to reduce the allocation of temporary objects in the heap The number, I will define a local variable in a method body, and the variable does not escape during the execution of the method. According to the JVM tuning mechanism, an instance of the class will first be created in the heap memory, and then the reference of this object will be pushed into Call stack, continue execution, this is the way before JVM optimization. Then, I use escape analysis to optimize the JVM. That is, for the reallocation method of the stack, first find the non-escaped variable and store the variable directly on the stack without entering the heap. After the allocation is completed, continue to call the stack for execution. Finally, the thread execution ends, the stack space is recycled, and the 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 and optimizing performance.

How to escape

Method escape: In a method body, define a local variable, which may be referenced by an external method, such as being passed to a method as a calling parameter. Or returned directly as an object. Or, it can be understood that the object jumps out of the method.

Thread escape: This object is accessed by other threads, such as assigned to an instance variable and accessed by other threads. The object escaped the current thread.

Benefits of Escape Analysis

If an object does not escape within the method body or within the thread (or after passing escape analysis, it fails to occur) Escape)

1. Allocation on the stack

Generally, objects that do not escape occupy a relatively large space. If the space on the stack can be used, a large number of objects will be allocated with the method. Destroyed at the end, reducing GC pressure

2. Synchronous elimination

If there is a synchronization lock on the method of the class you define, but at runtime, only one thread is accessing it, at this time The machine code after escape analysis will run without synchronization lock.

3. Scalar replacement

Java虚拟机中的原始数据类型(int,long等数值类型以及reference类型等)都不能再进一步分解,它们可以称为标量。相对的,如果一个数据可以继续分解,那它称为聚合量,Java中最典型的聚合量是对象。如果逃逸分析证明一个对象不会被外部访问,并且这个对象是可分解的,那程序真正执行的时候将可能不创建这个对象,而改为直接创建它的若干个被这个方法使用到的成员变量来代替。拆散后的变量便可以被单独分析与优化,可以各自分别在栈帧或寄存器上分配空间,原本的对象就无需整体分配空间了。

开启设置

在JDK 6u23以上是默认开启,这里将设置重新明确一下:
强制开启:   

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

关闭逃逸分析:    

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

写在结尾

栈上的空间一般而言是非常小的,只能存放若干变化和小的数据结构,无法存储大容量数据。目前的实现都是采用不那么准确但是时间压力相对较小的算法来完成逃逸分析,这就可能导致效果不稳定。所以,逃逸分析的效果只能在特定场景下,满足高频和高数量的小容量的变量分配结构,才是合适的。

The above is the detailed content of What is JVM escape? Introduction to the principles of JVM escape analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete