1. 引言
在云时代, java的跨平台特性, 已如同鸡肋.
大Jar包应用, 启动慢, 占用内存多, 还需要另装jvm
是java应用的通病.
这给了go等现代语言, 在 “开发效率”, “运行效率”, “应用生态”, 等各方面, 全面赶超的机会.
java语言, java生态, 需要进一步快速的革新, 才能保证不在"云时代"落伍.
GraalVM是java在 “运行效率”, “应用生态” 的革命性尝试.
- 运行效率方面
采用AOT(ahead of time)编译技术, 编译成 “本地镜像” 的方式, 解决java应用, 启动慢, 占用内存多的问题.
- 应用生态方面
GraalVM打造成一个统一的虚拟机, 支持JVM-Based语言(java, scala, groovy), LLVM-based语言(C, C++), JavaScript等等.
2. GraalVM安装
- 安装graalvm
-
liuxianqiang-mac:work-on liuxianqiang$ java -version openjdk version "1.8.0_282" OpenJDK Runtime Environment (build 1.8.0_282-b07) OpenJDK 64-Bit Server VM GraalVM CE 21.0.0 (build 25.282-b07-jvmci-21.0-b06, mixed mode)
- 安装graalvm插件, native-image
gu install native-image
3. HelloWorld.java
package demo.graalvm;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world graalvm");
}
}
4. POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>demo-graalvm</groupId>
<artifactId>demo-graalvm</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<!-- 和本地graalvm安装的版本要一致 -->
<graalvm.version>21.0.0</graalvm.version>
</properties>
<dependencies>
<dependency>
<groupId>org.graalvm.sdk</groupId>
<artifactId>graal-sdk</artifactId>
<version>${graalvm.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>native-image-maven-plugin</artifactId>
<version>${graalvm.version}</version>
<executions>
<execution>
<goals>
<goal>native-image</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<skip>false</skip>
<imageName>demo-graalvm</imageName>
<mainClass>demo.graalvm.HelloWorld</mainClass>
<buildArgs>
--no-fallback
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
5. 本地镜像构建
- 方式一: 命令行 (
native-image demo.graalvm.HelloWorld
)
liuxianqiang-mac:classes liuxianqiang$ pwd
/Users/liuxianqiang/Documents/study-graalvm/demo-graalvm/target/classes
liuxianqiang-mac:classes liuxianqiang$
liuxianqiang-mac:classes liuxianqiang$ native-image demo.graalvm.HelloWorld
[demo.graalvm.helloworld:65433] classlist: 850.62 ms, 1.21 GB
[demo.graalvm.helloworld:65433] (cap): 1,292.84 ms, 1.21 GB
[demo.graalvm.helloworld:65433] setup: 2,962.00 ms, 1.47 GB
[demo.graalvm.helloworld:65433] (clinit): 146.90 ms, 1.47 GB
[demo.graalvm.helloworld:65433] (typeflow): 2,626.62 ms, 1.47 GB
[demo.graalvm.helloworld:65433] (objects): 2,681.47 ms, 1.47 GB
[demo.graalvm.helloworld:65433] (features): 118.94 ms, 1.47 GB
[demo.graalvm.helloworld:65433] analysis: 5,679.70 ms, 1.47 GB
[demo.graalvm.helloworld:65433] universe: 354.35 ms, 1.47 GB
[demo.graalvm.helloworld:65433] (parse): 529.86 ms, 1.47 GB
[demo.graalvm.helloworld:65433] (inline): 936.86 ms, 1.62 GB
[demo.graalvm.helloworld:65433] (compile): 3,928.74 ms, 1.94 GB
[demo.graalvm.helloworld:65433] compile: 5,708.03 ms, 1.94 GB
[demo.graalvm.helloworld:65433] image: 405.53 ms, 1.94 GB
[demo.graalvm.helloworld:65433] write: 248.77 ms, 1.94 GB
[demo.graalvm.helloworld:65433] [total]: 16,568.45 ms, 1.94 GB
liuxianqiang-mac:classes liuxianqiang$
liuxianqiang-mac:classes liuxianqiang$ ll
total 5800
drwxr-xr-x 3 liuxianqiang staff 96 Feb 27 23:08 demo/
-rwxr-xr-x 1 liuxianqiang staff 2967416 Feb 27 23:23 demo.graalvm.helloworld*
liuxianqiang-mac:classes liuxianqiang$
liuxianqiang-mac:classes liuxianqiang$ ./demo.graalvm.helloworld
hello world graalvm
- 方式二: maven 插件 (如上pom已配置,
native-image-maven-plugin
)
liuxianqiang-mac:demo-graalvm liuxianqiang$ pwd
/Users/liuxianqiang/Documents/study-graalvm/demo-graalvm
liuxianqiang-mac:demo-graalvm liuxianqiang$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< demo-graalvm:demo-graalvm >----------------------
[INFO] Building demo-graalvm 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
...
...
...
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ demo-graalvm ---
[INFO] Building jar: /Users/liuxianqiang/Documents/study-graalvm/demo-graalvm/target/demo-graalvm-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- native-image-maven-plugin:21.0.0:native-image (default) @ demo-graalvm ---
[INFO] ImageClasspath Entry: demo-graalvm:demo-graalvm:jar:1.0-SNAPSHOT (file:///Users/liuxianqiang/Documents/study-graalvm/demo-graalvm/target/demo-graalvm-1.0-SNAPSHOT.jar)
[INFO] Executing: /Library/Java/JavaVirtualMachines/graalvm-ce-java8-21.0.0/Contents/Home/jre/lib/svm/bin/native-image -cp /Users/liuxianqiang/Documents/study-graalvm/demo-graalvm/target/demo-graalvm-1.0-SNAPSHOT.jar --no-fallback -H:Class=demo.graalvm.HelloWorld -H:Name=demo-graalvm
[demo-graalvm:66404] classlist: 812.87 ms, 1.19 GB
[demo-graalvm:66404] (cap): 979.05 ms, 1.19 GB
[demo-graalvm:66404] setup: 2,305.00 ms, 1.69 GB
[demo-graalvm:66404] (clinit): 124.99 ms, 1.74 GB
[demo-graalvm:66404] (typeflow): 2,722.93 ms, 1.74 GB
[demo-graalvm:66404] (objects): 2,542.28 ms, 1.74 GB
[demo-graalvm:66404] (features): 140.10 ms, 1.74 GB
[demo-graalvm:66404] analysis: 5,644.08 ms, 1.74 GB
[demo-graalvm:66404] universe: 303.09 ms, 1.74 GB
[demo-graalvm:66404] (parse): 429.89 ms, 1.74 GB
[demo-graalvm:66404] (inline): 748.32 ms, 1.80 GB
[demo-graalvm:66404] (compile): 3,266.69 ms, 1.90 GB
[demo-graalvm:66404] compile: 4,719.13 ms, 1.90 GB
[demo-graalvm:66404] image: 401.99 ms, 1.90 GB
[demo-graalvm:66404] write: 265.57 ms, 1.91 GB
[demo-graalvm:66404] [total]: 14,825.78 ms, 1.91 GB
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16.879 s
[INFO] Finished at: 2021-02-27T23:29:55+08:00
[INFO] ------------------------------------------------------------------------
liuxianqiang-mac:demo-graalvm liuxianqiang$ ll target/
total 9648
drwxr-xr-x 4 liuxianqiang staff 128 Feb 27 23:23 classes/
-rwxr-xr-x 1 liuxianqiang staff 4016008 Feb 27 23:29 demo-graalvm*
-rw-r--r-- 1 liuxianqiang staff 888299 Feb 27 23:29 demo-graalvm-1.0-SNAPSHOT.jar
drwxr-xr-x 3 liuxianqiang staff 96 Feb 27 23:08 generated-sources/
drwxr-xr-x 3 liuxianqiang staff 96 Feb 27 23:08 maven-archiver/
drwxr-xr-x 3 liuxianqiang staff 96 Feb 27 23:08 maven-status/
liuxianqiang-mac:demo-graalvm liuxianqiang$ ./target/demo-graalvm
hello world graalvm