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

# or Install latest GraalVM CE
$ sdk i java 24-graalce

Build

# Build the native image
$ ./gradlew nativeCompile [-Pquick]

# 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

# 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=25 \
        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
    # Set HeapDump path
    $ build/native/nativeCompile/native-image-playground -XX:HeapDumpPath=$HOME/heapdump.hprof
  • Object/Shared Lib Details

    # Show shared libs (MacOS)
    $ otool -L build/native/nativeCompile/native-image-playground
    
    # Show shared libs (Linux)
    $ ldd build/native/nativeCompile/native-image-playground
    $ objdump -p build/native/nativeCompile/native-image-playground | grep NEEDED
    $ libtree -v 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

Resources