Understand JVM escape analysis in 3 minutes
As a qualified Java developer knows, basically all objects are created on the heap. However, there is still no absolute word here, it refers to basically all .
Yesterday during an interview, a friend said that all objects are created in the heap, and then laughed at the interviewer.
Let’s start our text. Let’s talk about escape analysis today.
Escape Analysis (Escape Analysis) is currently a relatively cutting-edge optimization technology in the Java virtual machine. This is a cross-function global data flow analysis algorithm that can effectively reduce synchronization load and memory heap allocation pressure in Java programs. 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.
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 call parameter to other Among methods, this is called method escape; it may even be accessed by external threads, such as assigning to instance variables that can be accessed in other threads. This is called thread escape; from never escape, method escape to thread escape, It is called the different escape degrees of objects from low to high.
Turn on escape analysis, the compiler can optimize the code as follows:
Synchronous elimination: If an object is found by escape analysis, it can only be Accessed by a thread, the operations on this object can be asynchronous. Allocation on the stack: If you are sure that an object will not escape from the thread, it would be a very good idea to allocate memory for the object on the stack. The memory space can be destroyed as the stack frame is popped. Scalar replacement: If an object is found by escape analysis to not be accessed by external methods, and the object can be dismantled, then the object may not be created when the program is actually executed. Instead create it directly using several member variables instead of this method. After splitting the object, the member variables of the object can be allocated and read and written on the stack.
In the JVM, you can specify whether to enable escape analysis through the following parameters:
-XX: DoEscapeAnalysis: Indicates that escape is enabled Analysis (enabled by default after JDK 1.7).
-XX:-DoEscapeAnalysis: Indicates turning off escape analysis.
Synchronization elimination
Thread synchronization itself is a relatively time-consuming process. If escape analysis can determine that a variable will not escape the thread and cannot be used by other Thread access, 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.
Such as the following code:
public void method() { Object o = new Object(); synchronized (o) { System.out.println(o); } }
Locks the object o
, but the life cycle of object o is the same as the method method(), so it will not be accessed by other threads , thread safety issues will not occur, then the JIT compilation phase will be optimized as follows:
public void method() { Object o = new Object(); System.out.println(o); }
This is also called lock elimination.
Allocation on the stack
In the Java virtual machine, almost all Java programmers know that the memory space for creating objects is allocated on the Java heap. Common sense, objects in the Java heap are shared and visible to each thread. As long as you hold a reference to this object, you can access the object data stored in the heap. The garbage collection subsystem of the virtual machine will recycle objects that are no longer used in the heap, but the recycling action, whether it is marking and filtering out recyclable objects, or recycling and organizing memory, requires a lot of resources. However, there is a special case. If escape analysis confirms that the object will not escape out of the thread, it may be optimized to allocation on the stack. This eliminates the need to allocate memory on the heap and eliminate the need for garbage collection.
Such as the following code:
public static void main(String[] args) throws InterruptedException { for (int i = 0; i < 1000000; i++) { alloc(); } Thread.sleep(100000); } private static void alloc() { User user = new User(); }
The code is very simple, it is to create 1 million times in a loop and use the alloc() method to create 1 million User objects. The User object defined in the alloc() method here is not referenced by other methods, so it meets the requirements for allocation on the stack.
The JVM parameters are as follows:
-Xmx2G -Xms2G -XX:+DoEscapeAnalysis -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError
Start the program and check the number of instances through the jmap tool:
jmap -histo pid num #instances #bytes class name ---------------------------------------------- 1: 3771 2198552 [B 2: 10617 1722664 [C 3: 104057 1664912 com.miracle.current.lock.StackAllocationTest$User
We can see that the program has created a total of 104057 User objects, which is far less than 100 Ten thousand. We can turn off escape analysis and look at it again:
-Xmx2G -Xms2G -XX:-DoEscapeAnalysis -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError
Start the program and check the number of instances through the jmap tool:
jmap -histo 42928 num #instances #bytes class name ---------------------------------------------- 1: 628 22299176 [I 2: 1000000 16000000 com.miracle.current.lock.StackAllocationTest$User
You can see that a total of 1 million User objects were created after turning off escape analysis. In comparison, allocation on the stack plays an important role in heap memory consumption and GC.
Scalar replacement
If a data can no longer be decomposed into smaller data to represent, the original data type in the Java virtual machine (numeric types such as int, long and reference types, etc.) cannot be further decomposed, then these data can be called scalars. In contrast, if a piece of data can continue to be decomposed, it is called an aggregate. Objects in Java are typical aggregates.
If escape analysis can prove that an object will not be accessed outside the method, and this object can be dismantled, then when the program is actually executed, it may not create this object, but directly create several of it. Member variables used by this method instead.
has the following code:
public static void main(String[] args) { method(); } private static void method() { User user = new User(25); System.out.println(user.age); } private static class User { private int age; public User(int age) { this.age = age; } }
在method()
方法中创建User对象,指定age为25,这里User不会被其他方法引用,也就是说它不会逃逸出方法,并且User是可以拆解为标量的。所以alloc()
代码会优化为如下:
private static void alloc() { int age = 25; System.out.println(age); }
总结
尽管目前逃逸分析技术仍在发展之中,未完全成熟,但它是即时编译器优化技术的一个重要前进方向,在日后的Java虚拟机中,逃逸分析技术肯定会支撑起一系列更实用、有效的优化技术。
The above is the detailed content of Understand JVM escape analysis in 3 minutes. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This project is designed to facilitate developers to monitor multiple remote host JVMs faster. If your project is Spring boot, it is very easy to integrate. Just introduce the jar package. If it is not Spring boot, don’t be discouraged. You can quickly initialize a Spring boot program and introduce it yourself. Jar package is enough

JVM command line parameters allow you to adjust JVM behavior at a fine-grained level. The common parameters include: Set the Java heap size (-Xms, -Xmx) Set the new generation size (-Xmn) Enable the parallel garbage collector (-XX:+UseParallelGC) Reduce the memory usage of the Survivor area (-XX:-ReduceSurvivorSetInMemory) Eliminate redundancy Eliminate garbage collection (-XX:-EliminateRedundantGCs) Print garbage collection information (-XX:+PrintGC) Use the G1 garbage collector (-XX:-UseG1GC) Set the maximum garbage collection pause time (-XX:MaxGCPau

An introduction to the analysis of the functions and principles of the JVM virtual machine: The JVM (JavaVirtualMachine) virtual machine is one of the core components of the Java programming language, and it is one of the biggest selling points of Java. The role of the JVM is to compile Java source code into bytecodes and be responsible for executing these bytecodes. This article will introduce the role of JVM and how it works, and provide some code examples to help readers understand better. Function: The main function of JVM is to solve the problem of portability of Java programs on different platforms.

Key points and precautions for mastering JVM memory usage JVM (JavaVirtualMachine) is the environment in which Java applications run, and the most important one is the memory management of the JVM. Properly managing JVM memory can not only improve application performance, but also avoid problems such as memory leaks and memory overflows. This article will introduce the key points and considerations of JVM memory usage and provide some specific code examples. JVM memory partitions JVM memory is mainly divided into the following areas: Heap (He

Before writing a java program to check whether the JVM is 32-bit or 64-bit, let us first discuss about the JVM. JVM is a java virtual machine, responsible for executing bytecode. It is part of the Java Runtime Environment (JRE). We all know that java is platform independent, but JVM is platform dependent. We need separate JVM for each operating system. If we have the bytecode of any java source code, we can easily run it on any platform due to JVM. The entire process of java file execution is as follows - First, we save the java source code with .java extension and the compiler converts it into bytecode with .class extension. This happens at compile time. Now, at runtime, J

The garbage collection mechanism of jvm is GC (Garbage Collection), also called garbage collector. Basic principles of GC: Recycle objects that are no longer used in memory; the method used for recycling in GC is called the collector. Since GC needs to consume some resources and time, Java analyzes the life cycle characteristics of the object and follows the Objects are collected in the new generation and old generation to shorten the pause caused by GC to the application as much as possible.

Java is a popular programming language. During the development of Java applications, you may encounter JVM memory overflow errors. This error usually causes the application to crash, affecting the user experience. This article will explore the causes of JVM memory overflow errors and how to deal with and avoid such errors. What is JVM memory overflow error? The Java Virtual Machine (JVM) is the running environment for Java applications. In the JVM, memory is divided into multiple areas, including heap, method area, stack, etc. The heap is used to store created objects

How to modify JVM memory configuration in Tomcat8 Tomcat does not recommend configuring variables directly in catalina.sh. Instead, write them in setenv.sh in the same directory as catalina (bin directory). So if we want to modify the memory configuration of the jvm, then we need to modify the setenv.sh file (not by default, you need to create a new setenv.sh) and write (modify the size according to your own situation): exportCATALINA_OPTS="$CATALINA_OPTS-Xms1000m" exportCATALINA_OPTS ="$CATALINA
