Is Ahead-Of-Time compilation available in Java 9?

4.3k views Asked by At

As per JEP 295

AOT compilation of any JDK modules, classes, or of user code, is experimental and not supported in JDK 9.

To use the AOTed java.base module, the user will have to compile the module and copy the resulting AOT library into the JDK installation directory or specify it on java command line.

I am confused with the above statement, if AOT is not supported in JDK 9 then how can we compile the module using AOT?

My second question is why AOT not supported in JDK 9 if it has many advantages over JIT?

1

There are 1 answers

2
Naman On

Its an Experimental feature.

To answer the latter part first from your question. AOT is not fully fledged in terms of compatibility with Java 9 APIs. Some of its restrictions also listed in the linked JEP are:

  • It's still restricted to 64 bit Linux based systems.

  • To use AOT compilation, users need to use the same JDK for compilation and execution. Version information about jaotc used for compilation is added as a part of the libraries and are checked during load time. If the Java runtime is updated, you need to recompile the AOT compiled modules before executing them. Mismatch in JDK versions used for compilation and execution may result in application crashes.

  • Lambda expressions and other complex concepts of Java, which uses dynamically generated classes at runtime are not currently supported by AOT compiler.

  • To generate shared object (.so) files, the system needs libelf to be pre-installed.

  • The logical compilation mode for java.base is tiered AOT since JIT recompilation of java.base methods is desired to reach peak performance. Only in certain scenarios does a non-tiered AOT compilation make sense. This includes applications which require predictable behavior, when footprint is more important than peak performance, or for systems where dynamic code generation is not allowed. In these cases, AOT compilation needs to be done on the entire application and is thus experimental in JDK 9.

These limitations can be addressed in future releases and that is when I am pretty sure the Experimental tag from the feature would be removed.


if AOT is not supported in JDK 9 then how can we compile the module using AOT?

To make use of the AOT, the application code needs to be compiled using jaotc compiler considering few of the restrictions as listed above. As stated in Ahead-of-Time Compilation: AOT Usage if an AOT library has been compiled using the tool as:

jaotc --output libHelloWorld.so HelloWorld.class

it can be used in the execution phase

java -XX:AOTLibrary=./libHelloWorld.so HelloWorld

provided same release JVM configurations are used during both compile time and runtime.

Once the execution is triggered using the above command, the use of AOT compiled files in by default ON. To toggle whether or not making use of those files a new argument has been introduced that can be used in the execution phase. i.e -

-XX:+/-UseAOT    

More importantly to relate to both of your questions above and even as clearly mentioned in Risks and Assumption section of the proposal:

If a user finds that an application starts up more slowly, or doesn't reach the expected peak performance, or crash, they can just switch AOT off with the -XX:-UseAOT flag, or remove any AOT libraries.