Spring6 AOT ahead of time compilation

1. Overview of AOT

insert image description here

1.1, the difference between JIT and AOT

The terms JIT and AOT refer to two different compilation methods. The main difference between these two compilation methods is whether to compile at " runtime "

(1) JIT, Just-in-time, dynamic (just-in-time) compilation, compiling while running;

When the program is running, the hot code is calculated according to the algorithm, and then JIT is compiled in real time. This method has high throughput, has a runtime performance bonus, can run faster, and can dynamically generate code, etc., but relatively startup The speed is slow, and it takes a certain amount of time and calling frequency to trigger the JIT's layering mechanism. The disadvantage of JIT is that compilation needs to occupy runtime resources, which will cause the process to freeze.

(2) AOT, Ahead Of Time, refers to pre-compilation and pre-compilation.

AOT compilation can directly convert source code into machine code, with low memory usage and fast startup speed . It can run without runtime and directly statically link runtime to the final program, but there is no runtime performance bonus, and it cannot be modified according to the running status of the program. Further optimization, the disadvantage of AOT is that compiling the program before running will increase the installation time of the program.

To put it simply: JIT just-in-time compilation refers to the process of converting bytecode into machine code that can be directly run on the hardware during the running of the program, and deploying it to the hosting environment. AOT compilation refers to the process of converting bytecode into machine code before the program runs .

.java -> .class -> (使用jaotc编译工具) -> .so(程序函数库,即编译好的可以供其他程序使用的代码和数据)

insert image description here

(3) Advantages of AOT

To put it simply, the Java virtual machine load has been precompiled into a binary library that can be executed directly. There is no need to wait for the warm-up of the just-in-time compiler, reducing the bad experience of "running slowly at the first time" for Java applications.

Compile before the program runs to avoid compilation performance consumption and memory consumption at runtime. The
highest performance can be achieved at the early stage of program running, and the program startup speed is fast. The
running product is only machine code, and the package size is small.

Disadvantages of AOT

Because it is compiled statically ahead of time, machine instruction sequences cannot be selected according to hardware conditions or program running conditions. The theoretical peak performance is not as good as JIT, and there
is no dynamic capability. The same product cannot run across platforms.

The first, just-in-time compilation (JIT), is the default mode and is used by the Java Hotspot virtual machine to convert bytecode to machine code at runtime. The latter ahead-of-time compilation (AOT) is supported by the novel GraalVM compiler and allows direct static compilation of bytecode to machine code at build time.

Now we are in the era of cloud native, cost reduction and efficiency increase. Compared with other programming languages ​​such as Go and Rust, Java has a very big drawback that the compilation and startup process is very slow. Native technologies conflict with each other. Spring6 uses AOT technology to achieve low memory usage and fast startup speed, gradually meeting the needs of Java in the cloud native era. For commercial companies that use Java applications on a large scale, they can consider using JDK17 as soon as possible. Native technology reduces costs and increases efficiency for the company.

1.2、Graalvm

The AOT technology supported by Spring6, this GraalVM is the underlying support, and Spring also provides first-class support for GraalVM native images. GraalVM is a high-performance JDK designed to accelerate the execution of applications written in Java and other JVM languages, while also providing runtimes for JavaScript, Python, and many other popular languages. GraalVM offers two ways to run Java applications: on the HotSpot JVM using Graal's just-in-time (JIT) compiler or as an ahead-of-time (AOT) compiled native executable. GraalVM's polyglot capabilities make it possible to mix multiple programming languages ​​within a single application while eliminating the cost of foreign language calls. GraalVM adds an advanced just-in-time (JIT) optimizing compiler written in Java to the HotSpot Java virtual machine.

GraalVM has the following features :

(1) An advanced optimizing compiler that generates faster, leaner code that requires fewer computing resources

(2) AOT native image compilation compiles Java applications into native binaries in advance, starts immediately, and achieves the highest performance without warming up

(3) Polyglot programming utilizes the best features and libraries of popular languages ​​in a single application without additional overhead

(4) Advanced tools to debug, monitor, analyze and optimize resource consumption in Java and multiple languages

Generally speaking, the requirements for cloud native are not high. You can continue to use the 2.7.X version and JDK8 in the short term, but Spring has officially released Spring6.

1.3、Native Image

At present, in addition to the AOT solution in the JVM, there is another way to implement Java AOT in the industry, which is to directly abandon the JVM, and compile the code into machine code directly through the compiler like C/C++, and then run it. This is undoubtedly an idea that directly subverts the design of the Java language, that is, GraalVM Native Image. It implements an ultra-miniature runtime component——Substrate VM through C language, which basically realizes various features of JVM, but is lightweight enough to be easily embedded, which frees Java language and engineering from the limitations of JVM. It can truly realize the same AOT compilation as C/C++. After a long period of optimization and accumulation, this solution has achieved very good results, and has basically become Oracle's official Java AOT solution.
Native Image is an innovative technology that compiles Java code into a stand-alone native executable or native shared library. The Java bytecode processed during the building of the native executable includes all application classes, dependencies, third-party dependent libraries, and any required JDK classes. The resulting self-contained native executables are specific to each individual operating system and machine architecture that does not require a JVM.

2. Native Image construction process

2.1, GraalVM installation

(1) Download GraalVM

Enter the official website to download: https://www.graalvm.org/downloads/

insert image description here

insert image description here

(2) Configure environment variables

Add GRAALVM_HOME

insert image description here

Change JAVA_HOME to the location of graalvm

insert image description here

Change the Path to the bin location of graalvm

insert image description here

Use the command to check whether the installation is successful

insert image description here

(3) Install the native-image plugin

Use the command gu install native-image to download and install

insert image description here

2.2. Install the C++ compilation environment

(1) Download the Visual Studio installation software

https://visualstudio.microsoft.com/zh-hans/downloads/

insert image description here

(2) Install Visual Studio

insert image description here

insert image description here

(3) Add Visual Studio environment variables

Configure INCLUDE, LIB and Path

insert image description here

insert image description here

insert image description here

(4) Open the tool and operate in the tool

insert image description here

2.3. Write code and build Native Image

(1) Write Java code

public class Hello {
    
    

    public static void main(String[] args) {
    
    
        System.out.println("hello world");
    }
}

(2) Copy the file to the directory and execute the compilation

insert image description here

(3) Native Image for construction

insert image description here

insert image description here

(4) View the built files

insert image description here

(5) Execute the built file

insert image description here

It can be seen that the binary file size of the final packaged output of Hello is 11M, which is the size after including various libraries of SVM and JDK. Although it is larger than the binary file of C/C++, it is larger than the complete JVM. It can be said that it is already very small.

Compared with running with JVM, Native Image is much faster and takes up less cpu. From the various experimental data provided by the government, it can be seen that Native Image has greatly improved the startup speed and memory usage. obviously:

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/weixin_43847283/article/details/131362157