Power node SpringBoot3 notes ⑨ understand AOT and GraalVM

9 Understanding AOT and GraalVM

9.1 Technologies that provide performance

What is AOT :
Ahead-of-Time Compilation: Pre-compilation (ahead of compilation) It is described in JEP-295 and added as an experimental feature in Java 9.
AOT is a way to improve the performance of Java programs, especially the startup time of the JVM. Compile Java classes to native code before starting the virtual machine. Improve startup time for small and large Java applications.

JIT (just in time) :
JIT is a technology for JVM to improve execution speed. JVM executes Java bytecode and compiles frequently executed code into native code. This is called just-in-time (JIT) compilation.
When the JVM finds that a certain method or code block runs particularly frequently, it will consider it a "hot spot code". Then JIT will compile the "hot code" into machine code related to the local machine, optimize it, and then cache the compiled machine code for next use.
The JVM decides which code to JIT compile based on profiling information collected during execution . JIT compilers are fast, but Java programs are so large that it takes a long time for the JIT to fully warm up. Infrequently used Java methods may not be compiled at all.

Features: When the program is executed, compile while running the code. JIT compilation requires time overhead and space overhead. Only code that is executed frequently is worth compiling.

AOT: Static
JIT: Dynamic

9.2 Native Image

native image: native image (native image)

native image is a technology to ahead-of-time compile Java code to a standalone executable, called a
native image ). Images are files for execution.
A mirror file (native image) is generated through the mirror construction technology (tool).
Native image is not only a technical term but also refers to his generated executable file.
native image supports jvm-based languages, such as Java, Scala, Clojure, Kotlin

The content of the native image file includes application classes, classes from its dependencies, runtime library classes, and statically linked native code from JDK (binary files can be run directly without additional JDK installation), and the native image runs on GraalVM , with faster startup time and lower runtime memory overhead.

In AOT mode, the compiler performs all the compilation work during the build of the project, the main idea here is to move all the "heavy lifting" - expensive calculations - to build time. That is to prepare everything that the project must execute, such as the specific execution classes, files, etc. Finally, the prepared file is executed, and the application can start quickly at this time. Reduce memory, cpu overhead (no runtime JIT compilation). Because everything is precomputed and precompiled.

9.3 Native Image builder

Native Image builder (image builder): is a utility for processing all the classes of the application and their dependencies, including classes from the JDK. It statically analyzes this data to determine which classes and methods are accessible during application execution. It then pre-compiles the reachable code and data into native executables for specific operating systems and architectures

9.4 GraalVM

GraalVM is a high-performance JDK distribution designed to accelerate applications written in Java and other JVM languages, while supporting JavaScript, Ruby, Python, and many other popular languages. 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 is a virtual machine that supports multiple languages.

GraalVM is an alternative to OpenJDK that includes a tool called native image that supports ahead-of-time (AOT) compilation. GraalVM executes native image files faster, uses less CPU and memory, and has a smaller disk size. This makes Java more competitive in the cloud

Currently, the focus of AOT is to allow Spring applications to be deployed as native images using GraalVM. The GraalVM solution is used in Spring Boot 3 to provide Native Image support.

GraalVM's "native image" tool takes Java bytecode as input and outputs a native executable. In order to do this, the tool performs static analysis on the bytecode. During analysis, the tool looks for all the code that your application actually uses and eliminates everything that is unnecessary. Native image is a closed static analysis and compilation, and does not support dynamic loading of classes. All dependencies required for program operation are completed in the static analysis phase

9.5 Operation steps of AOT:

Use SpringBoot or Spring to create a project, GraalVM's native image build generates a native image file, and execute the image file on GraalVM.

Guess you like

Origin blog.csdn.net/f5465245/article/details/130152164