Skip to content

sureshg/native-image-playground

Repository files navigation

GraalVM Native Image Playground

GitHub Workflow Status GraalVM CE Kotlin release Style guide

GraalVM Native Image of a kotlin/java app and publish the platform binaries using GitHub action.

Install GraalVM CE Dev

# Install GraalVM CE Dev
$ ./scripts/graalvm-ce-dev.sh

# Install GraalVM CE
$ curl -s "https://get.sdkman.io" | bash
$ sdk i java 22.0.1-graalce

Build

# Build the native image
$ ./gradlew nativeCompile

# Use trace agent for metadata generation
$ ./gradlew -Pagent run [--rerun-tasks]
# Gracefully shutdown the server instead of killing Gradle run.
$ curl http://localhost:9080/shutdown
$ ./gradlew metadataCopy

# Run native image tests
$ ./gradlew nativeTest
$ ./gradlew -Pagent nativeTest

# Native Image Quick Build
$ ./gradlew nativeCompile -Pquick

# Build Native Image Bundles
$ ./gradlew nativeCompile -Pbundle
$ native-image --bundle-apply="build/native/nativeCompile/native-image-playground.nib"
$ build/native-image-playground.output/default/native-image-playground

# Build by disabling the build cache
$ ./gradlew clean nativeCompile --rerun-tasks --no-build-cache

# GraalVM JIT Mode
$ ./gradlew build
$ java --enable-preview \
       --add-modules=ALL-SYSTEM \
       -jar build/libs/native-image-playground-*-all.jar

# Find out the classes/jars using top modules mentioned in the native-image build output
$ jdeps -q \
        -R \
        --ignore-missing-deps \
        --multi-release=24 \
        build/libs/native-image-playground-*-all.jar

# Build native image from modular jars
$ native-image \
    -p base-module.jar:main-module.jar \
    -m dev.suresh.Main

Run & Debugging

  • Using Distroless

    # Download https://github.com/sureshg/native-image-playground/releases/latest and extract it
    $ chmod +x native-image-playground
    
    # Running "mostly static native image" built on GithubAction (Linux amd64)
    $ docker run \
           -it \
           --rm \
           --platform=linux/amd64 \
           --pull always \
           --publish 9080:9080 \
           --name native-image-playground \
           --mount type=bind,source=$(pwd),destination=/app,readonly \
           --entrypoint=/app/native-image-playground \
           gcr.io/distroless/base
    
    # Running static image compiled using musl libc
    $ docker run \
           -it \
           --rm \
           --pull always \
           --publish 9080:9080 \
           --name native-image-playground \
           --mount type=bind,source=$(pwd),destination=/app,readonly \
           --entrypoint=/app/native-image-playground \
           gcr.io/distroless/static
    
    # To kill the container
    $ docker kill native-image-playground
  • List all runtime options

    $ build/native/nativeCompile/native-image-playground -XX:PrintFlags= 2>&1
    
    # Eg: Set HeapDump path
    $ build/native/nativeCompile/native-image-playground -XX:HeapDumpPath=$HOME/heapdump.hprof
  • Object/Shared Lib Details

    # Show shared libs
    $ otool -L build/native/nativeCompile/native-image-playground
    
    # SVM details
    $ strings -a build/native/nativeCompile/native-image-playground | grep -i com.oracle.svm.core.VM
    
    # Show all bundled CA Certs
    $ strings -a build/native/nativeCompile/native-image-playground | grep -i "cn="
  • Mach-O Format Viewer

  • Misc Gradle Tasks

    # Detect unused and misused dependencies
    $ ./gradlew buildHealth
    $ cat build/reports/dependency-analysis/build-health-report.txt
    
    $ ./gradlew reason --id org.jetbrains.kotlin:kotlin-stdlib

Resources