いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

GraalVMが全機能無償で使えるようになった (GraalVM Free Terms and Conditions (GFTC) license)

概要

以下、自分用のメモです。

Oracle の GraalVM が以下の記事を出していました。

medium.com

PublicKeyさんの方でも記事なってますね。

www.publickey1.jp

本番環境でもオッケイという状態になってます。素晴らしい。

せっかくなので、ちょっと試してみましてみて、その内容を以下のリポジトリにアップしておきました。

よかったら、ご参考まで。Gitpod上で試せるようにしています。

github.com

試してみる

以下は上記リポジトリの内容です。

インストール

$ wget https://download.oracle.com/graalvm/20/latest/graalvm-jdk-20_linux-x64_bin.tar.gz
$ tar zxf graalvm-jdk-*.tar.gz
$ mv graalvm-jdk-*/ graalvm
$ graalvm/bin/gu install native-image

ソースコード

こういうときは、まずはへろーワールドからです。

public class App {
    public static void main(String[] args) {
        System.out.println("hello world");
    }
}

タスクファイル

taskfile.dev

を使ってます。

# https://taskfile.dev

version: "3"

vars:
  DIR_GRAALVM: "{{.PWD}}/graalvm/bin"
  CMD_GU: "{{.DIR_GRAALVM}}/gu"
  CMD_JAVAC: "{{.DIR_GRAALVM}}/javac"
  CMD_NATIVEIMAGE: "{{.DIR_GRAALVM}}/native-image"

tasks:
  install-nativeimage:
    internal: true
    cmds:
      - "{{.CMD_GU}} install native-image"
  helloworld:
    deps: [install-nativeimage]
    dir: helloworld
    cmds:
      - rm -f ./app ./App.class
      - "{{.CMD_JAVAC}} App.java"
      - "{{.CMD_NATIVEIMAGE}} -Ob App"
      - file ./app
      - ./app

実行してみる

$ task helloworld
task: [install-nativeimage] /workspace/java-graalvm-native-image-example/graalvm/bin/gu install native-image
Picked up JAVA_TOOL_OPTIONS:  -Xmx3489m
Downloading: Artifacts catalog from gds.oracle.com
Skipping ULN EE channels, no username provided.
Downloading: Component catalog from www.graalvm.org
Processing Component: Native Image
Component Native Image (org.graalvm.native-image) is already installed.
task: [helloworld] rm -f ./app ./App.class
task: [helloworld] /workspace/java-graalvm-native-image-example/graalvm/bin/javac App.java
Picked up JAVA_TOOL_OPTIONS:  -Xmx3489m
task: [helloworld] /workspace/java-graalvm-native-image-example/graalvm/bin/native-image -Ob App
========================================================================================================================
GraalVM Native Image: Generating 'app' (executable)...
========================================================================================================================
[1/8] Initializing...                                                                                    (7.4s @ 0.16GB)
 Java version: 20.0.1+9, vendor version: Oracle GraalVM 20.0.1+9.1
 Graal compiler: optimization level: b, target machine: x86-64-v3, PGO: off
 C compiler: gcc (linux, x86_64, 11.3.0)
 Garbage collector: Serial GC (max heap size: 80% of RAM)
[2/8] Performing analysis...  [*****]                                                                   (13.0s @ 0.29GB)
   2,042 (62.05%) of  3,291 types reachable
   2,021 (46.71%) of  4,327 fields reachable
   9,660 (39.63%) of 24,374 methods reachable
     655 types,   106 fields, and   419 methods registered for reflection
      49 types,    32 fields, and    48 methods registered for JNI access
       4 native libraries: dl, pthread, rt, z
[3/8] Building universe...                                                                               (2.1s @ 0.27GB)
[4/8] Parsing methods...      [**]                                                                       (2.1s @ 0.38GB)
[5/8] Inlining methods...     [***]                                                                      (1.3s @ 0.28GB)
[6/8] Compiling methods...    [***]                                                                      (7.8s @ 0.30GB)
[7/8] Layouting methods...    [*]                                                                        (1.0s @ 0.26GB)
[8/8] Creating image...       [*]                                                                        (1.6s @ 0.33GB)
   2.28MB (37.79%) for code area:     5,415 compilation units
   3.41MB (56.50%) for image heap:   56,678 objects and 1 resources
 352.72kB ( 5.71%) for other data
   6.04MB in total
------------------------------------------------------------------------------------------------------------------------
Top 10 origins of code area:                                Top 10 object types in image heap:
   1.20MB java.base                                          484.79kB byte[] for code metadata
 892.84kB svm.jar (Native Image)                             471.33kB byte[] for java.lang.String
  86.61kB com.oracle.svm.svm_enterprise                      377.09kB java.lang.String
  18.42kB org.graalvm.nativeimage.base                       325.55kB java.lang.Class
  17.37kB jdk.internal.vm.ci                                 263.68kB byte[] for general heap data
  14.99kB jdk.proxy1                                         163.75kB java.util.HashMap$Node
  14.88kB org.graalvm.sdk                                    112.63kB char[]
  13.15kB jdk.proxy3                                          94.62kB byte[] for reflection metadata
   4.97kB jdk.internal.vm.compiler                            87.89kB java.lang.Object[]
   3.62kB jdk.proxy2                                          79.77kB com.oracle.svm.core.hub.DynamicHubCompanion
   54.00B for 1 more packages                                538.33kB for 565 more object types
------------------------------------------------------------------------------------------------------------------------
Recommendations:
 G1GC: Use the G1 GC ('--gc=G1') for improved latency and throughput.
 PGO:  Use Profile-Guided Optimizations ('--pgo') for improved throughput.
 HEAP: Set max heap for improved and more predictable memory usage.
 CPU:  Enable more CPU features with '-march=native' for improved performance.
 BRPT: Try out the new build reports with '-H:+BuildReport'.
------------------------------------------------------------------------------------------------------------------------
                        1.5s (3.9% of total time) in 33 GCs | Peak RSS: 1.07GB | CPU load: 3.61
------------------------------------------------------------------------------------------------------------------------
Produced artifacts:
 /workspace/java-graalvm-native-image-example/helloworld/app (executable)
========================================================================================================================
Finished generating 'app' in 37.0s.
task: [helloworld] file ./app
./app: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=702622388ce481b5798334d9c14c9d0f60b3b418, for GNU/Linux 3.2.0, with debug_info, not stripped
task: [helloworld] ./app
hello world

結構コンパイルに時間がかかりますね。

ちゃんと実行ファイルが出来てるのが確認できます。

参考情報

www.graalvm.org

www.graalvm.org


過去の記事については、以下のページからご参照下さい。

サンプルコードは、以下の場所で公開しています。