Home Java javaTutorial How to understand escape in Java

How to understand escape in Java

Apr 28, 2023 pm 07:19 PM
java

In the Java compilation system, the process of turning a Java source code file into a computer-executable machine instruction requires two stages of compilation. The first stage is to convert the .java file into a .class file. The second stage of compilation is the process of converting .class into machine instructions.

The first section of compilation is the javac command.

In the second compilation stage, the JVM interprets the bytecode and translates it into corresponding machine instructions, reads it in one by one, and interprets the translation one by one. Obviously, after interpretation and execution, its execution speed will inevitably be much slower than the executable binary bytecode program. This is the function of the traditional JVM interpreter (Interpreter). In order to solve this efficiency problem, JIT (just in time compilation) technology was introduced.

After the introduction of JIT technology, Java programs are still interpreted and executed through the interpreter. When the JVM finds that a certain method or code block is running particularly frequently, it will consider it to be "Hot Spot Code". Then JIT will translate part of the "hot code" into machine code related to the local machine, optimize it, and then cache the translated machine code for next use.

Since I have already introduced the content of JIT compilation and hotspot detection in my in-depth analysis of Java compilation principles, I will not go into details here. This article mainly introduces the optimization in JIT. One of the most important aspects of JIT optimization is escape analysis.

Escape analysis

Regarding the concept of escape analysis, you can refer to the fact that not all objects and arrays allocate memory on the heap. One article, here is a brief review:

The basic behavior of escape analysis is to 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 to other places as a call parameter, which is called method escape.

For example, the following code:

public static StringBuffer craeteStringBuffer(String s1, String s2) {

StringBuffer sb = new StringBuffer();

sb.append(s1);

sb.append(s2);

Return sb;

}

public static String createStringBuffer(String s1, String s2) {

StringBuffer sb = new StringBuffer();

sb.append(s1);

sb.append(s2);

Return sb.toString();

}

The sb in the first code escapes, but the sb in the second code does not escape.

Using escape analysis, the compiler can optimize the code as follows:

1. Synchronization is omitted. If an object is found to be accessible only from one thread, synchronization may not be considered for operations on this object.

2. Convert heap allocation to stack allocation. If an object is allocated in a subroutine, so that the pointer to the object never escapes, the object may be a candidate for stack allocation rather than heap allocation.

3. Separate objects or scalar replacement. Some objects may not need to exist as a continuous memory structure to be accessed, so part (or all) of the object may not be stored in memory, but stored in CPU registers.

When Java code is running, you can specify whether to enable escape analysis through JVM parameters,

-XX: DoEscapeAnalysis: Indicates turning on escape analysis

-XX:-DoEscapeAnalysis: Indicates turning off escape analysis. Starting from jdk 1.7, escape analysis has been started by default. If you want to turn it off, you need to specify -XX:-DoEscapeAnalysis

Synchronization omitted

When dynamically compiling a synchronized block, the JIT compiler can use escape analysis to determine whether the lock object used by the synchronized block can only be accessed by one thread and has not been released to other threads.

If the lock object used by the synchronized block is confirmed to be only accessible by one thread through this analysis, the JIT compiler will desynchronize this part of the code when compiling the synchronized block. This process of canceling synchronization is called synchronization omission, also called lock elimination.

Such as the following code:

public void f() {

Object hollis = new Object();

synchronized(hollis) {

System.out.println(hollis);

}

}

The hollis object is locked in the code, but the life cycle of the hollis object is only in the f() method and will not be accessed by other threads, so it will be optimized during the JIT compilation phase. Optimized to:

public void f() {

Object hollis = new Object();

System.out.println(hollis);

}

Therefore, when using synchronized, if the JIT finds that there is no thread safety problem after escape analysis, it will eliminate the lock.

Scalar replacement

Scalar refers to a data that cannot be broken down into smaller data. The primitive data type in Java is scalar. In contrast, data that can be decomposed is called an aggregate. An object in Java is an aggregate because it can be decomposed into other aggregates and scalars.

In the JIT stage, if it is found through escape analysis that an object will not be accessed by the outside world, then after JIT optimization, the object will be disassembled into several member variables contained in it and replaced. This process is scalar replacement.

public static void main(String[] args) {

alloc();

}

private static void alloc() {

Point point = new Point(1,2);

System.out.println("point.x=" point.x "; point.y=" point.y);

}

class Point{

private int x;

private int y;

}

In the above code, the point object does not escape the alloc method, and the point object can be disassembled into scalars. Then, JIT will not directly create the Point object, but directly use two scalars int x and int y to replace the Point object.

The above code, after scalar replacement, will become:

private static void alloc() {

int x = 1;

int y = 2;

System.out.println("point.x=" x "; point.y=" y);

}

It can be seen that after the escape analysis of the aggregate quantity Point, it was found that it did not escape, so it was replaced by two aggregate quantities. So what are the benefits of scalar substitution? That is, it can greatly reduce the heap memory usage. Because once there is no need to create objects, there is no need to allocate heap memory.

Scalar substitution provides a good basis for allocation on the stack.

Allocation on stack

In the Java virtual machine, it is common knowledge that objects are allocated memory in the Java heap. However, there is a special case, that is, if after escape analysis it is found that an object does not have an escape method, it may be optimized into allocation on the stack. This eliminates the need to allocate memory on the heap and eliminate the need for garbage collection.

For a detailed introduction to allocation on the stack, please refer to Not all objects and arrays allocate memory on the heap. .

Here, I would like to briefly mention that in existing virtual machines, allocation on the stack is not really implemented, and objects and arrays are not all allocated on the heap. In our example, the object is not allocated on the heap, it is actually implemented by scalar replacement.

Escape analysis is not mature

The paper on escape analysis was published in 1999, but it was not implemented until JDK 1.6, and this technology is not very mature yet.

The fundamental reason is that there is no guarantee that the performance consumption of escape analysis will be higher than its consumption. Although escape analysis can do scalar substitution, stack allocation, and lock elimination. However, escape analysis itself also requires a series of complex analyses, which is actually a relatively time-consuming process.

An extreme example is that after escape analysis, it is found that no object does not escape. Then the process of escape analysis is wasted.

Although this technology is not very mature, it is also a very important means in just-in-time compiler optimization technology.

The above is the detailed content of How to understand escape in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles