Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 738
Mohit Tyagi Avatar asked Oct 05 '17 06:10

Mohit Tyagi


People also ask

What is ahead of time compilation in Java?

Ahead-Of-Time (AOT) compilation allows the compilation of Java™ classes into native code for subsequent executions of the same program. The AOT compiler works with the class data sharing framework.

Does Java use JIT or AOT?

There are two main compilation strategies in Java: Just in Time Compilation (JIT) and Ahead of Time Compilation (AOT). The first generates machine code during the execution of the program itself (i.e., shortly before the first invocation of a Java method).

Is there a Java version 9?

Java SE 9 is the result of an industry-wide development effort involving open review, weekly builds, and extensive collaboration between Oracle engineers and members from the worldwide Java developer community via the OpenJDK Community and the JCP.


1 Answers

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.

like image 142
Naman Avatar answered Oct 08 '22 10:10

Naman