GraalVM与Spring Native初体验

安装GraalVM

下载地址: https://github.com/graalvm/graalvm-ce-builds/releases/tag/vm-21.3.0. 根据我的主机环境, 选择 java11 MacOS (amd64) 下载.

解压

1
tar -zxf graalvm-ce-java11-darwin-amd64-21.3.0.tar.gz

配置环境变量

1
vi ~/.bash_profile

加入(替换原JAVA_HOME配置)

1
2
export PATH=/Users/cuishiying/app/graalvm-ce-java11-21.3.0/Contents/Home/bin:$PATH
export JAVA_HOME=/Users/cuishiying/app/graalvm-ce-java11-21.3.0/Contents/Home

应用并生效

1
source ~/.bash_profile

查看是否生效

1
2
➜  bin echo $JAVA_HOME
/Users/cuishiying/app/graalvm-ce-java11-21.3.0/Contents/Home
1
2
3
4
➜  bin java -version
openjdk version "11.0.13" 2021-10-19
OpenJDK Runtime Environment GraalVM CE 21.3.0 (build 11.0.13+7-jvmci-21.3-b05)
OpenJDK 64-Bit Server VM GraalVM CE 21.3.0 (build 11.0.13+7-jvmci-21.3-b05, mixed mode, sharing)

Hello World

新建1个java文件

1
touch HelloWorld.java

然后写入

1
2
3
4
5
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

JVM版

1
2
3
4
➜  desktop javac HelloWorld.java
➜ desktop time java HelloWorld
Hello, World!
java HelloWorld 0.10s user 0.03s system 129% cpu 0.104 total

GraalVM版

先进行安装 native-image

1
gu install native-image

查看已安装

1
2
3
4
5
6
7
8
9
➜  desktop gu list
ComponentId Version Component name Stability Origin
---------------------------------------------------------------------------------------------------------------------------------
graalvm 21.3.0 GraalVM Core Supported
js 21.3.0 Graal.js Supported
native-image 21.3.0 Native Image Early adopter github.com

➜ desktop native-image --version
GraalVM 21.3.0 Java 11 CE (Java Version 11.0.13+7-jvmci-21.3-b05)

然后在刚刚编译HelloWorld的目录下进行执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
➜  desktop native-image HelloWorld
[helloworld:38788] classlist: 1,606.07 ms, 0.96 GB
[helloworld:38788] (cap): 3,521.96 ms, 0.96 GB
[helloworld:38788] setup: 5,748.94 ms, 0.96 GB
[helloworld:38788] (clinit): 207.66 ms, 1.72 GB
[helloworld:38788] (typeflow): 4,566.81 ms, 1.72 GB
[helloworld:38788] (objects): 3,792.94 ms, 1.72 GB
[helloworld:38788] (features): 1,429.65 ms, 1.72 GB
[helloworld:38788] analysis: 10,383.54 ms, 1.72 GB
[helloworld:38788] universe: 1,208.89 ms, 1.72 GB
[helloworld:38788] (parse): 1,254.93 ms, 1.76 GB
[helloworld:38788] (inline): 1,320.43 ms, 1.76 GB
[helloworld:38788] (compile): 15,208.22 ms, 2.38 GB
[helloworld:38788] compile: 18,858.23 ms, 2.38 GB
[helloworld:38788] image: 1,381.92 ms, 2.38 GB
[helloworld:38788] write: 524.57 ms, 2.38 GB
[helloworld:38788] [total]: 40,024.02 ms, 2.38 GB
# Printing build artifacts to: /Users/cuishiying/work/desktop/helloworld.build_artifacts.txt

等待一段时间后,我们会发现文件生成了

1
2
3
4
5
6
➜  desktop ll
total 21224
-rw-r--r-- 1 cuishiying staff 427B 6 3 11:59 HelloWorld.class
-rw-r--r-- 1 cuishiying staff 117B 6 3 11:37 HelloWorld.java
-rwxr-xr-x 1 cuishiying staff 10M 6 3 12:03 helloworld
-rw-r--r-- 1 cuishiying staff 25B 6 3 12:03 helloworld.build_artifacts.txt

完全没问题,再测试一下时间

1
2
3
➜  desktop time ./helloworld
Hello, World!
./helloworld 0.01s user 0.01s system 1% cpu 0.762 total