Ahead Of Time VS Just In Time in Java

saurav omar
4 min readMay 11, 2019

--

How Java works:

  • Java compiles the code and converts into byte code(.class)
  • JVM interprets the byte codes and converts into machine level codes so that it can run on any machine

Byte Code To Machine Code:

  • Byte code is get converted to machine level code using a dictionary of instructions (byte to m/c), because of different types of machines(like Ubuntu, mac, windows, etc) have different types of the instruction set.
  • The interpreter is very quick to start and load the app.
  • One problem is interpreter does not perform any kind of optimization, because of that same byte code get converted to machine code every time.
  • To solve this problem C1 Compiler is used, which uses code cache.

C1 Compiler:

  • So when interpreter interprets the byte code it uses counter how many times the same byte code gets converted to machine code which is saved in the code cache.
  • When the counter reaches the threshold then C1 compiler compiles the codes and save in code cache so that when same byte codes get executed it will get from code cache.
  • Code cache is a memory area separate from the JVM heap that contains all the JVM bytecode for a method compiled down to native code, each called a nmethod1. This is where the JIT compiled methods are kept.
  • This is also called JIT(just in time) compilation.
  • Default Code cache size is 240MB in java 8 but we can set a different value, using the flag -XReservedCodeCacheSize.
  • The default compilation threshold is 1500 for C1 Compiler

C2 Compiler:

  • After sometime when JVM runs for some time, its start collecting statistics in the background how the code is being executed called code profiling. Its creates control flow graphs(code paths). It tries to find out hottest code paths once it has enough statistics then JVM asked for C2 compiler to perform optimizations on the hottest code paths.
  • It also stores optimized code in Code Cache.

Optimizations Perform by C2:

  • Dead Code
  • Escape Analysis
  • Loops
  • Methods Inlining
  • Null check Elimination

etc are used by C2 compiler to optimize the hottest code

More about C1 and C2:

  • Two compilers, C1, and C2 run in parallel and keep on optimizing the code
  • . C1 is preferred for the client application and C2 is preferred for long running server applications.
  • Tiered compilation combines the best features of both compilers. Client-side compilation yields quick startup time and speedy optimization, while server-side compilation delivers more advanced optimizations later in the execution cycle.
  • When the Code Cache is constrained (its usage approaches or reaches the ReservedCodeCacheSize), to compile more methods, the JIT must first throw out some already compiled methods. Discarding compiled methods are known as Code Cache flushing.
  • In JAVA 7 we have the option to select to both the compiler.
  • In JAVA 8 both are available by default.

Ahead of Time Compilation:

While doing profiling manually or manually check thread or memory dump from JVM we might need to perform compilation ahead of time. this feature is enabled after JAVA9.

After Java9 we have the option to convert some of the classes or libraries to compiled code before the start of the application.

Compile class before:

Before we can use the AOT compiler, we need to compile the class with the Java compiler:

javac <classname>.java

Pass the class to the AOT compiler:

We then pass the resulting <classname>.java to the AOT compiler, which is located in the same directory as the standard Java compiler.

jaotc --output <classname>.so <classname>.class

Running the Program

We can then execute the program while running the program we need to use the flag -XX: AOTLibrary to tell the JVM for AOT compiled class.

java -XX:AOTLibrary=./<classname>.so <classname>
  • We can also see the library was loaded by adding -XX:+PrintAOT as a JVM argument.

That’s it.

Happy Learning.

--

--

saurav omar
saurav omar

Written by saurav omar

Geek and Always ready to give more than 100%

No responses yet