[JVM]-[In-depth understanding of Java virtual machine study notes]-Escape analysis

Preface

The basic principle of escape analysis is: analyze the dynamic scope of the object. When an object is defined in a method, it may be referenced by an external method, such as being passed as a calling parameter to other methods. This is called method escape ; even It may be accessed by an external thread, such as being assigned to an instance variable that can be accessed in other threads. This is called thread escape . The degree of escape of objects from low to high can be divided intoDon't escapemethod escapeas well asthread escape
If it can be proven that an object will not escape from a method or thread, that is, other methods or threads cannot access the object; or the degree of escape is relatively low, different levels of optimization can be adopted for the object.

Stack Allocation

Allocation on the stack means that if it is determined that an object will not escape from the thread, then let the object be allocated on the stack instead of the heap, so that the memory space occupied by the object can be allocated as the stack frame is popped. Destruction
If allocation on the stack is used, objects that will not escape or only method escape will occur ( allocation on the stack can support method escape but not thread escape , after all, the virtual machine stack is private to the thread) can be destroyed with the end of the method. Automatically destroy, rather than occupying space on the heap and leaving it to the garbage collector to recycle them. This way, the pressure on the garbage collector subsystem can be reduced a lot, and it also saves space on the heap.

Scalar Replacement

If a data can no longer be decomposed into smaller data to represent, such as primitive data types (int, long and other numerical types and reference type data in the virtual machine). Reference type data is different from objects. Reference type data saves the object's Address, this address value naturally cannot be split; but the object is different, it stores many variables, which can be split) cannot be further decomposed, then these data can be called scalars. Relatively, if a
data can continue to decompose, then it is called an aggregate ( A aggregate AggregateA g g r e g a t e ), objects in Java are typical aggregates.
A Java object is dismantled, and the member variables used are restored to their original types for access according to program access conditions. This process is calledwithscalar
If escape analysis can prove that an object will not bethe method, and this object can be dismantled, then the program can not create this object when executing, but instead directly create several of it that are accessed by this method. Member variables used
In this way, the member variables of the object can be allocated and read and written on the stack. Scalar substitution can be seen as a special case of allocation on the stack. It not only allocates the object on the stack, but also allocates only the member variables that will be used by the method. However, it requires a higher degree of escape and does not allow the object to escape to Outside the method, that is,only objects that do not escape are allowed. It can be understood that on the basis of allocation on the stack, the object will not only not escape from the thread, but also will not escape from the method, so we will optimize it to a greater extent, not only allocate it on the stack, but also dismantle it. Divided into scalar form, it reduces the number of objects created on the heap and the number of GC

Synchronization Elimination

If escape analysis can determine that a variable will not escape the thread and cannot be accessed by other threads, then there will definitely be no competition in reading and writing this variable, and the synchronization measures implemented on this variable can be safely eliminated. This saves the time-consuming operation of thread synchronization.

Guess you like

Origin blog.csdn.net/Pacifica_/article/details/124480997