开发环境
名称 | 版本 |
---|---|
操作系统 | Windows 11 X64 |
Visual Studio | Communty 2022 |
GraalVM | graalvm-community-jdk-17.0.9_windows-x64_bin.zip |
Native-Image | native-image-installable-svm-java17-windows-amd64-22.3.2.jar |
AOT 与 JIT
GraalVM
GraalVM 是一个高性能的 JDK,旨在加速用 Java 和其他 JVM 语言编写的应用程序的执行,
同时还提供 JavaScript、Python 和许多其他流行语言的运行时。GraalVM 提供了两种运行 Java 应用程序的方式:
● 1.在 HotSpot JVM 上使用 Graal即时(JIT)编译器
● 2.作为预先编译(AOT)的本机可执行文件运行(本地镜像)。GraalVM 的多语言能力使得在单个应用程序中混合多种编程语言成为可能,同时消除了外部语言调用的成本。
- 架构
跨平台提供原生镜像原理:
GraalVM - Windows 平台
安装 VisualStudio
下载地址:https://visualstudio.microsoft.com/zh-hans/free-developer-offers/
- 社区版
- 安装位置
C:\Program Files\Microsoft Visual Studio\2022\Community
- 安装成功
下载安装 GraalVM
- 下载
- 安装
解压 graalvm-community-jdk-17.0.9_windows-x64_bin.zip
到 C:\Program Files\Java\graalvm-community-openjdk-17.0.9+9.1
配置环境变量
变量 | 值 |
---|---|
Java_Home | C:\Program Files\Java\graalvm-community-openjdk-17.0.9+9.1 |
Path | %Java_Home%\bin |
验证 JDK 环境为 GraalVM 提供的即可
C:\Users\luoma>java -version
openjdk version "17.0.9" 2023-10-17
OpenJDK Runtime Environment GraalVM CE 17.0.9+9.1 (build 17.0.9+9-jvmci-23.0-b22)
OpenJDK 64-Bit Server VM GraalVM CE 17.0.9+9.1 (build 17.0.9+9-jvmci-23.0-b22, mixed mode, sharing)
C:\Users\luoma>gu --version
GraalVM Updater 23.0.2
下载安装 Native-Image 依赖
gu install native-image
D:\luoma\工具\Java\04_其它>gu install native-image
Downloading: Component catalog from www.graalvm.org
Processing Component: Native Image
Component Native Image (org.graalvm.native-image) is already installed.
新建项目-boot3-15-aot-common
新建模块
项 | 值 |
---|---|
名称 | boot3-15-aot-common |
位置 | D:\Study-Java\2023\SpringBoot3-Study |
语言 | Java |
构建系统 | Maven |
JDK | graalvm-17 GraalVM version 17.0.9 |
添加实例代码 | 勾选 |
组 | com.atguigu |
工件 | boot3-15-aot-common |
- 主方法类
package com.atguigu;
/**
* 打包成本地镜像
* 1.打成 Jar 包:注意修改 jar 包内的 MANIFEST.MF 文件,指定 Main-Class 的全类名
* - java -jar xxx.jar 就可以执行。
* - 切换机器,安装 java 环境,默认解释执行,启动速度慢,运行速度慢。
* 2.打成本地镜像(可执行文件):
* - Windows: exe
*
*/
public class MainApp {
public static void main(String[] args) {
System.out.println("Hello world1111!");
}
}
打包 boot3-15-aot-common
Maven
-boot3-15-aot-common
-生命周期
-clear
-package
[INFO] Building jar: D:\data\boot3-15-aot-common-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.905 s
[INFO] Finished at: 2024-01-11T11:17:25+08:00
[INFO] ------------------------------------------------------------------------
使用压缩工具打开
boot3-15-aot-common-1.0-SNAPSHOT.jar
修改
boot3-15-aot-common-1.0-SNAPSHOT.jar\META-INF\MANIFEST.MF
,添加主启动类
Manifest-Version: 1.0
Created-By: Maven JAR Plugin 3.3.0
Build-Jdk-Spec: 17
Main-Class: com.atguigu.MainApp
- 命令行进入
D://data
,输入
java -jar boot3-15-aot-common-1.0-SNAPSHOT.jar
输出
Hello world1111!
打包成功。
编译镜像
使用 jar 包编译
打开工具
x64 Native Tools Command Prompt for VS 2022
进入
D://data
输入下面代码
# boot3-15-aot-common-1.0-SNAPSHOT.jar 为 jar 包名称
# com.atguigu.MainApp 为主程序名称
# Haha 为输出文件名称
native-image -cp boot3-15-aot-common-1.0-SNAPSHOT.jar com.atguigu.MainApp -o Haha
- 输出
D:\data>native-image -cp boot3-15-aot-common-1.0-SNAPSHOT.jar com.atguigu.MainApp -o Haha
========================================================================================================================
GraalVM Native Image: Generating 'Haha' (executable)...
========================================================================================================================
For detailed information and explanations on the build output, visit:
https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/BuildOutput.md
------------------------------------------------------------------------------------------------------------------------
[1/8] Initializing... (10.9s @ 0.14GB)
Java version: 17.0.9+9, vendor version: GraalVM CE 17.0.9+9.1
Graal compiler: optimization level: 2, target machine: x86-64-v3
C compiler: cl.exe (microsoft, x64, 19.38.33134)
Garbage collector: Serial GC (max heap size: 80% of RAM)
[2/8] Performing analysis... [***] (7.7s @ 0.28GB)
2,900 (72.39%) of 4,006 types reachable
3,517 (50.90%) of 6,909 fields reachable
13,209 (44.24%) of 29,858 methods reachable
897 types, 0 fields, and 350 methods registered for reflection
62 types, 53 fields, and 52 methods registered for JNI access
1 native library: version
[3/8] Building universe... (1.1s @ 0.36GB)
[4/8] Parsing methods... [*] (0.7s @ 0.34GB)
[5/8] Inlining methods... [***] (0.6s @ 0.23GB)
[6/8] Compiling methods... [***] (5.9s @ 0.31GB)
[7/8] Layouting methods... [*] (1.1s @ 0.39GB)
[8/8] Creating image... [**] (2.2s @ 0.31GB)
4.56MB (38.80%) for code area: 7,522 compilation units
7.04MB (59.85%) for image heap: 92,458 objects and 5 resources
163.08kB ( 1.35%) for other data
11.76MB in total
------------------------------------------------------------------------------------------------------------------------
Top 10 origins of code area: Top 10 object types in image heap:
3.44MB java.base 1016.77kB byte[] for code metadata
858.95kB svm.jar (Native Image) 921.09kB java.lang.String
113.28kB java.logging 913.53kB byte[] for general heap data
62.87kB org.graalvm.nativeimage.base 674.43kB byte[] for java.lang.String
24.29kB jdk.internal.vm.ci 670.37kB java.lang.Class
23.48kB org.graalvm.sdk 356.78kB char[]
6.19kB jdk.internal.vm.compiler 354.80kB java.util.HashMap$Node
1.35kB jdk.proxy1 249.22kB com.oracle.svm.core.hub.DynamicHubCompanion
1.27kB jdk.proxy3 187.01kB java.lang.String[]
1.19kB jdk.localedata 166.00kB java.lang.Object[]
468.00B for 2 more packages 1.25MB for 827 more object types
------------------------------------------------------------------------------------------------------------------------
Recommendations:
HEAP: Set max heap for improved and more predictable memory usage.
CPU: Enable more CPU features with '-march=native' for improved performance.
------------------------------------------------------------------------------------------------------------------------
1.0s (3.3% of total time) in 157 GCs | Peak RSS: 0.87GB | CPU load: 3.83
------------------------------------------------------------------------------------------------------------------------
Produced artifacts:
D:\data\Haha.exe (executable)
========================================================================================================================
Finished generating 'Haha' in 30.7s.
D:\\data
目录中输出了一个Haha.exe
文件执行
Haha.exe
D:\data>Haha.exe
Hello world1111!
使用 class 文件编译
复制项目编译的
class
文件夹\classes\com\atguigu\MainApp.class
到D:\\data\\classes
打开工具
x64 Native Tools Command Prompt for VS 2022
进入
D://data
输入下面代码
# classes 为项目 class 文件夹
# com.atguigu.MainApp 为项目主程序
# Hehe 为输出的文件名称
D:\data>native-image -cp classes com.atguigu.MainApp -o Hehe
- 输出
D:\data>native-image -cp classes com.atguigu.MainApp -o Hehe
========================================================================================================================
GraalVM Native Image: Generating 'Hehe' (executable)...
========================================================================================================================
For detailed information and explanations on the build output, visit:
https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/BuildOutput.md
------------------------------------------------------------------------------------------------------------------------
[1/8] Initializing... (10.2s @ 0.14GB)
Java version: 17.0.9+9, vendor version: GraalVM CE 17.0.9+9.1
Graal compiler: optimization level: 2, target machine: x86-64-v3
C compiler: cl.exe (microsoft, x64, 19.38.33134)
Garbage collector: Serial GC (max heap size: 80% of RAM)
[2/8] Performing analysis... [***] (7.3s @ 0.28GB)
2,900 (72.39%) of 4,006 types reachable
3,517 (50.90%) of 6,909 fields reachable
13,209 (44.24%) of 29,858 methods reachable
897 types, 0 fields, and 350 methods registered for reflection
62 types, 53 fields, and 52 methods registered for JNI access
1 native library: version
[3/8] Building universe... (1.1s @ 0.32GB)
[4/8] Parsing methods... [*] (0.7s @ 0.37GB)
[5/8] Inlining methods... [***] (0.6s @ 0.21GB)
[6/8] Compiling methods... [***] (5.9s @ 0.35GB)
[7/8] Layouting methods... [*] (1.0s @ 0.37GB)
[8/8] Creating image... [**] (2.0s @ 0.30GB)
4.56MB (38.79%) for code area: 7,522 compilation units
7.04MB (59.85%) for image heap: 92,436 objects and 5 resources
163.56kB ( 1.36%) for other data
11.76MB in total
------------------------------------------------------------------------------------------------------------------------
Top 10 origins of code area: Top 10 object types in image heap:
3.44MB java.base 1017.04kB byte[] for code metadata
858.98kB svm.jar (Native Image) 921.09kB java.lang.String
113.28kB java.logging 913.54kB byte[] for general heap data
62.87kB org.graalvm.nativeimage.base 674.43kB byte[] for java.lang.String
24.29kB jdk.internal.vm.ci 670.37kB java.lang.Class
23.48kB org.graalvm.sdk 356.78kB char[]
6.19kB jdk.internal.vm.compiler 353.95kB java.util.HashMap$Node
1.35kB jdk.proxy1 249.22kB com.oracle.svm.core.hub.DynamicHubCompanion
1.27kB jdk.proxy3 187.01kB java.lang.String[]
1.19kB jdk.localedata 166.00kB java.lang.Object[]
468.00B for 2 more packages 1.25MB for 827 more object types
------------------------------------------------------------------------------------------------------------------------
Recommendations:
HEAP: Set max heap for improved and more predictable memory usage.
CPU: Enable more CPU features with '-march=native' for improved performance.
------------------------------------------------------------------------------------------------------------------------
1.1s (3.5% of total time) in 156 GCs | Peak RSS: 0.86GB | CPU load: 4.12
------------------------------------------------------------------------------------------------------------------------
Produced artifacts:
D:\data\Hehe.exe (executable)
========================================================================================================================
Finished generating 'Hehe' in 29.4s.
D:\\data
目录中输出了一个Hehe.exe
文件执行
Haha.exe
D:\data>Hehe.exe
Hello world1111!
GraalVM - Linux 平台
安装 gcc 等环境
yum install lrzsz
sudo yum install gcc glibc-devel zlib-devel
下载安装 Linux 下的 GraalVM
下载
graalvm-ce-java17-linux-amd64-22.3.2.tar.gz
- 安装
tar -zxvf graalvm-ce-java17-linux-amd64-22.3.2.tar.gz -C /opt/java/
sudo vim /etc/profile
#修改以下内容
export JAVA_HOME=/opt/java/graalvm-ce-java17-22.3.2
export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile
下载安装 Linux 下的 Native-Image
下载
native-image-installable-svm-java17-linux-amd64-22.3.2.jar
- 安装
gu install --file native-image-installable-svm-java17-linux-amd64-22.3.2.jar
使用 Native-Image 编译 jar 为原生程序
native-image -cp xxx.jar org.example.App -o Demo
- 编译成功后,运行
./Demo