SpringBoot3-13.AOT-GraalVM

2024年01月10日 17:56 · 阅读(220) ·

开发环境

名称 版本
操作系统 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

AOT 与 JIT

GraalVM

官网:https://www.graalvm.org/

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 for JDK 17 Community 17.0.9

  • 安装

解压 graalvm-community-jdk-17.0.9_windows-x64_bin.zipC:\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 提供的即可

  1. C:\Users\luoma>java -version
  2. openjdk version "17.0.9" 2023-10-17
  3. OpenJDK Runtime Environment GraalVM CE 17.0.9+9.1 (build 17.0.9+9-jvmci-23.0-b22)
  4. OpenJDK 64-Bit Server VM GraalVM CE 17.0.9+9.1 (build 17.0.9+9-jvmci-23.0-b22, mixed mode, sharing)
  5. C:\Users\luoma>gu --version
  6. GraalVM Updater 23.0.2

下载安装 Native-Image 依赖

  1. gu install native-image
  1. D:\luoma\工\Java\04_其它>gu install native-image
  2. Downloading: Component catalog from www.graalvm.org
  3. Processing Component: Native Image
  4. 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
  • 主方法类
  1. package com.atguigu;
  2. /**
  3. * 打包成本地镜像
  4. * 1.打成 Jar 包:注意修改 jar 包内的 MANIFEST.MF 文件,指定 Main-Class 的全类名
  5. * - java -jar xxx.jar 就可以执行。
  6. * - 切换机器,安装 java 环境,默认解释执行,启动速度慢,运行速度慢。
  7. * 2.打成本地镜像(可执行文件):
  8. * - Windows: exe
  9. *
  10. */
  11. public class MainApp {
  12. public static void main(String[] args) {
  13. System.out.println("Hello world1111!");
  14. }
  15. }

打包 boot3-15-aot-common

  • Maven-boot3-15-aot-common-生命周期-clear-package
  1. [INFO] Building jar: D:\data\boot3-15-aot-common-1.0-SNAPSHOT.jar
  2. [INFO] ------------------------------------------------------------------------
  3. [INFO] BUILD SUCCESS
  4. [INFO] ------------------------------------------------------------------------
  5. [INFO] Total time: 0.905 s
  6. [INFO] Finished at: 2024-01-11T11:17:25+08:00
  7. [INFO] ------------------------------------------------------------------------
  • 使用压缩工具打开 boot3-15-aot-common-1.0-SNAPSHOT.jar

  • 修改 boot3-15-aot-common-1.0-SNAPSHOT.jar\META-INF\MANIFEST.MF,添加主启动类

  1. Manifest-Version: 1.0
  2. Created-By: Maven JAR Plugin 3.3.0
  3. Build-Jdk-Spec: 17
  4. Main-Class: com.atguigu.MainApp
  • 命令行进入 D://data,输入
  1. java -jar boot3-15-aot-common-1.0-SNAPSHOT.jar

输出

  1. Hello world1111!

打包成功。

编译镜像

使用 jar 包编译

  • 打开工具 x64 Native Tools Command Prompt for VS 2022

  • 进入 D://data

  • 输入下面代码

  1. # boot3-15-aot-common-1.0-SNAPSHOT.jar 为 jar 包名称
  2. # com.atguigu.MainApp 为主程序名称
  3. # Haha 为输出文件名称
  4. native-image -cp boot3-15-aot-common-1.0-SNAPSHOT.jar com.atguigu.MainApp -o Haha
  • 输出
  1. D:\data>native-image -cp boot3-15-aot-common-1.0-SNAPSHOT.jar com.atguigu.MainApp -o Haha
  2. ========================================================================================================================
  3. GraalVM Native Image: Generating 'Haha' (executable)...
  4. ========================================================================================================================
  5. For detailed information and explanations on the build output, visit:
  6. https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/BuildOutput.md
  7. ------------------------------------------------------------------------------------------------------------------------
  8. [1/8] Initializing... (10.9s @ 0.14GB)
  9. Java version: 17.0.9+9, vendor version: GraalVM CE 17.0.9+9.1
  10. Graal compiler: optimization level: 2, target machine: x86-64-v3
  11. C compiler: cl.exe (microsoft, x64, 19.38.33134)
  12. Garbage collector: Serial GC (max heap size: 80% of RAM)
  13. [2/8] Performing analysis... [***] (7.7s @ 0.28GB)
  14. 2,900 (72.39%) of 4,006 types reachable
  15. 3,517 (50.90%) of 6,909 fields reachable
  16. 13,209 (44.24%) of 29,858 methods reachable
  17. 897 types, 0 fields, and 350 methods registered for reflection
  18. 62 types, 53 fields, and 52 methods registered for JNI access
  19. 1 native library: version
  20. [3/8] Building universe... (1.1s @ 0.36GB)
  21. [4/8] Parsing methods... [*] (0.7s @ 0.34GB)
  22. [5/8] Inlining methods... [***] (0.6s @ 0.23GB)
  23. [6/8] Compiling methods... [***] (5.9s @ 0.31GB)
  24. [7/8] Layouting methods... [*] (1.1s @ 0.39GB)
  25. [8/8] Creating image... [**] (2.2s @ 0.31GB)
  26. 4.56MB (38.80%) for code area: 7,522 compilation units
  27. 7.04MB (59.85%) for image heap: 92,458 objects and 5 resources
  28. 163.08kB ( 1.35%) for other data
  29. 11.76MB in total
  30. ------------------------------------------------------------------------------------------------------------------------
  31. Top 10 origins of code area: Top 10 object types in image heap:
  32. 3.44MB java.base 1016.77kB byte[] for code metadata
  33. 858.95kB svm.jar (Native Image) 921.09kB java.lang.String
  34. 113.28kB java.logging 913.53kB byte[] for general heap data
  35. 62.87kB org.graalvm.nativeimage.base 674.43kB byte[] for java.lang.String
  36. 24.29kB jdk.internal.vm.ci 670.37kB java.lang.Class
  37. 23.48kB org.graalvm.sdk 356.78kB char[]
  38. 6.19kB jdk.internal.vm.compiler 354.80kB java.util.HashMap$Node
  39. 1.35kB jdk.proxy1 249.22kB com.oracle.svm.core.hub.DynamicHubCompanion
  40. 1.27kB jdk.proxy3 187.01kB java.lang.String[]
  41. 1.19kB jdk.localedata 166.00kB java.lang.Object[]
  42. 468.00B for 2 more packages 1.25MB for 827 more object types
  43. ------------------------------------------------------------------------------------------------------------------------
  44. Recommendations:
  45. HEAP: Set max heap for improved and more predictable memory usage.
  46. CPU: Enable more CPU features with '-march=native' for improved performance.
  47. ------------------------------------------------------------------------------------------------------------------------
  48. 1.0s (3.3% of total time) in 157 GCs | Peak RSS: 0.87GB | CPU load: 3.83
  49. ------------------------------------------------------------------------------------------------------------------------
  50. Produced artifacts:
  51. D:\data\Haha.exe (executable)
  52. ========================================================================================================================
  53. Finished generating 'Haha' in 30.7s.
  • D:\\data 目录中输出了一个 Haha.exe 文件

  • 执行 Haha.exe

  1. D:\data>Haha.exe
  2. Hello world1111!

使用 class 文件编译

  • 复制项目编译的 class 文件夹 \classes\com\atguigu\MainApp.classD:\\data\\classes

  • 打开工具 x64 Native Tools Command Prompt for VS 2022

  • 进入 D://data

  • 输入下面代码

  1. # classes 为项目 class 文件夹
  2. # com.atguigu.MainApp 为项目主程序
  3. # Hehe 为输出的文件名称
  4. D:\data>native-image -cp classes com.atguigu.MainApp -o Hehe
  • 输出
  1. D:\data>native-image -cp classes com.atguigu.MainApp -o Hehe
  2. ========================================================================================================================
  3. GraalVM Native Image: Generating 'Hehe' (executable)...
  4. ========================================================================================================================
  5. For detailed information and explanations on the build output, visit:
  6. https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/BuildOutput.md
  7. ------------------------------------------------------------------------------------------------------------------------
  8. [1/8] Initializing... (10.2s @ 0.14GB)
  9. Java version: 17.0.9+9, vendor version: GraalVM CE 17.0.9+9.1
  10. Graal compiler: optimization level: 2, target machine: x86-64-v3
  11. C compiler: cl.exe (microsoft, x64, 19.38.33134)
  12. Garbage collector: Serial GC (max heap size: 80% of RAM)
  13. [2/8] Performing analysis... [***] (7.3s @ 0.28GB)
  14. 2,900 (72.39%) of 4,006 types reachable
  15. 3,517 (50.90%) of 6,909 fields reachable
  16. 13,209 (44.24%) of 29,858 methods reachable
  17. 897 types, 0 fields, and 350 methods registered for reflection
  18. 62 types, 53 fields, and 52 methods registered for JNI access
  19. 1 native library: version
  20. [3/8] Building universe... (1.1s @ 0.32GB)
  21. [4/8] Parsing methods... [*] (0.7s @ 0.37GB)
  22. [5/8] Inlining methods... [***] (0.6s @ 0.21GB)
  23. [6/8] Compiling methods... [***] (5.9s @ 0.35GB)
  24. [7/8] Layouting methods... [*] (1.0s @ 0.37GB)
  25. [8/8] Creating image... [**] (2.0s @ 0.30GB)
  26. 4.56MB (38.79%) for code area: 7,522 compilation units
  27. 7.04MB (59.85%) for image heap: 92,436 objects and 5 resources
  28. 163.56kB ( 1.36%) for other data
  29. 11.76MB in total
  30. ------------------------------------------------------------------------------------------------------------------------
  31. Top 10 origins of code area: Top 10 object types in image heap:
  32. 3.44MB java.base 1017.04kB byte[] for code metadata
  33. 858.98kB svm.jar (Native Image) 921.09kB java.lang.String
  34. 113.28kB java.logging 913.54kB byte[] for general heap data
  35. 62.87kB org.graalvm.nativeimage.base 674.43kB byte[] for java.lang.String
  36. 24.29kB jdk.internal.vm.ci 670.37kB java.lang.Class
  37. 23.48kB org.graalvm.sdk 356.78kB char[]
  38. 6.19kB jdk.internal.vm.compiler 353.95kB java.util.HashMap$Node
  39. 1.35kB jdk.proxy1 249.22kB com.oracle.svm.core.hub.DynamicHubCompanion
  40. 1.27kB jdk.proxy3 187.01kB java.lang.String[]
  41. 1.19kB jdk.localedata 166.00kB java.lang.Object[]
  42. 468.00B for 2 more packages 1.25MB for 827 more object types
  43. ------------------------------------------------------------------------------------------------------------------------
  44. Recommendations:
  45. HEAP: Set max heap for improved and more predictable memory usage.
  46. CPU: Enable more CPU features with '-march=native' for improved performance.
  47. ------------------------------------------------------------------------------------------------------------------------
  48. 1.1s (3.5% of total time) in 156 GCs | Peak RSS: 0.86GB | CPU load: 4.12
  49. ------------------------------------------------------------------------------------------------------------------------
  50. Produced artifacts:
  51. D:\data\Hehe.exe (executable)
  52. ========================================================================================================================
  53. Finished generating 'Hehe' in 29.4s.
  • D:\\data 目录中输出了一个 Hehe.exe 文件

  • 执行 Haha.exe

  1. D:\data>Hehe.exe
  2. Hello world1111!

GraalVM - Linux 平台

安装 gcc 等环境

  1. yum install lrzsz
  2. sudo yum install gcc glibc-devel zlib-devel

下载安装 Linux 下的 GraalVM

下载 graalvm-ce-java17-linux-amd64-22.3.2.tar.gz

  • 安装
  1. tar -zxvf graalvm-ce-java17-linux-amd64-22.3.2.tar.gz -C /opt/java/
  2. sudo vim /etc/profile
  3. #修改以下内容
  4. export JAVA_HOME=/opt/java/graalvm-ce-java17-22.3.2
  5. export PATH=$PATH:$JAVA_HOME/bin
  6. source /etc/profile

下载安装 Linux 下的 Native-Image

下载 native-image-installable-svm-java17-linux-amd64-22.3.2.jar

  • 安装
  1. gu install --file native-image-installable-svm-java17-linux-amd64-22.3.2.jar

使用 Native-Image 编译 jar 为原生程序

  1. native-image -cp xxx.jar org.example.App -o Demo
  • 编译成功后,运行 ./Demo