This project shows how to generate a native image using GraalVM.
To use it:
-
Set the
JAVA_HOMEenvironment variable to the path of a GraalVM installation, -
Set the current working directory to the folder containing this file,
-
Generate the artifact locally with:
../mvnw clean verify -Puse-<backend>
where
<backend>can be:simple-
This profile uses
SimpleLoggeras logging backend. jul-
This profile uses
java.util.loggingas logging backend. logback-
This profile uses Logback as logging backend.
log4j-core-
This profile uses Log4j Core as logging backend with its out-of-the-box reachability metadata. It uses Pattern Layout to format log events.
log4j-core-jtl-
This profile uses Log4j Core as logging backend with its out-of-the-box reachability metadata. It uses JSON Template Layout to format log events.
log4j-core-minimal-
This profile uses Log4j Core a logging backend with an external reachability metadata source optimized for size. It uses Pattern Layout to format log events.
log4j-core-jtl-minimal-
This profile uses Log4j Core a logging backend with an external reachability metadata source optimized for size. It uses JSON Template Layout to format log events.
-
Run the application with
target/log4j-samples-graalvm
-
Log4j, as many other Java libraries, relies heavily on reflection to convert the configuration file into an instance of a Configuration object. The classes that are accessed through reflection are called Log4j plugins and GraalVM must be aware of their existence through the usage of reachability metadata.
See Log4j plugins and GraalVM reachability metadata for more details.
The
Log4j plugin descriptors
and
GraalVM reachability metadata used in the customized
use-log4j-core-minimal
and
use-log4j-core-jtl-minimal
profiles were generated using the procedure described in the documentation of the
log4j-converter-plugin-descriptor tool.
Using GraalVM 21.0.4 on Linux x64, the native binaries have the following size:
|
Caution
|
Since this is a trivial Java application, the difference between the binary size without and with Log4j is large: when Log4j is added GraalVM must compile many additional In a typical application, those classes are already required, so the difference should be smaller. |
| Configuration | Size |
|---|---|
With |
13.90 MiB |
With |
14.10 MiB |
With Logback |
30.62 MiB |
With Log4j Core (manually optimized reachability metadata) |
36.19 MiB |
With Log4j Core + JSON Template Layout (manually optimized reachability metadata) |
37.40 MiB |
With Log4j Core (embedded full reachability metadata) |
46.80 MiB |
With Log4j Core + JSON Template Layout (embedded full reachability metadata) |
47.37 MiB |