Java JIT Compiler

Faruk BOZAN
emlakjet
Published in
2 min readFeb 15, 2022

Welcome on the post about Java JIT (just-in-time) compiler. I will not write about “what is compiler”, because this section is very detail and long. It deserves a separate post.

As we know, some modern programming languages are converted into machine or assembly code by compiler. WORA(Write Once Run Anywhere) languages like Java has a middle layer. A classical Java code process is like below.

Code —> javac —> bytecode —> JVM —> OS

The code written by you converted into bytecode format with javac command. And then this bytecode is used by JVM (Java Virtual Machine) to OS oriented operations.

In this point we can define compiler in two categories: Static and dynamic compiler.

Static compiler: This compiler type produces same output while inputs are not change. So it is fast. And javac is static compiler.

Dynamic compiler: This compiler produces outputs not with only inputs. Also some other parameters like usage frequency are used. It is very efficient in runtime. JIT is a dynamic compiler.

Let’s dive deeper with snorkel. JIT converts bytecode blocks into native code in runtime. In other view, OS dependent code. The main goal is increasing performance of most used and called blocks. JVM hold counters for most calling codes or blocks. JIT will take the control if this counter exceeds a threshold. JIT converts bytecode into native code and no way back.

This threshold value can be given by parameter and also you can disable JIT. Also you can assign thread count while JIT works. This thread count can be dangerous because more and more thread means less space for other operations.

Now we will have a look on JIT process. JIT does some refinements on code for us. Here are the some of them.

Dead code: Unused codes are eliminated by JIT.

Inlining: Smaller codes means jump command for native code. So JIT joins some small codes in to bigger one to reduce jump counts.

Local optimization: JIT does some performance improvements in methods or blocks.

Control flow optimization: JIT does some performance improvements in if, for, while and etc.

Global optimization: JIT does some performance improvements due to whole method.

Native code generation: And this is last step. Here your native code.

Thanks for reading.

--

--