JVM basically runs the compiled Java byte codes(e.g. Main.class). In the early Java system, JVM was running each line of code line by line when it’s needed. Reading line by line of the bytecodes and interpreting it into the machine-readable language was definitely making Java slower than languages like C.
Programming languages like C doesn’t need an interpreter or other additional software that will interpret the code and make it runnable. This is because C is being compiled to native machine code, which is comprehended by the machine itself.
To overcome the slowness, JVM introduced a feature called Just in Time compilation called as JIT compilation.
Just In Time (JIT) compilation
First, JVM will monitor which methods/codes are being run frequently.
For example, let’s say we have a class that runs Line3~Line5 for couple hundred times. Then, the Line3~Line5 will be compiled into native machine code which will make it faster to run. These native machine codes are the ones that can be directly interpreted by Operating System(e.g. Windows, Mac). While other lines remain as compiled bytecode, Line 3 to Line 5 will be native machine code.
One of the most important implication here is that code will run faster the longer it is left to run. This is because JVM needs to optimize the code which will improve when it runs longer.
The sequence of making codes into native machine codes runs in a separate thread, which means the process of JIT compiling doesn’t stop the application running. While the compilation is taking place, the JVM will continue to use the interpreted version.
Compiler Flags
-XX:+PrintCompilation-XX: Advanced option+: Switch to turn this option ON. ('-'to turn OFF)PrintCompilation: Flag name to see what kind of compilation is happening when JVM is running our Java codes
- Columns Data:
- The number of milliseconds since the virtual machine started
- Order of method/code was compiled
- n: native method, s: synchronized method, !: exception handling, %: code is natively compiled and running in a memory called code cache
- What kind of compilation has take place represented by 0~4
- 0: No compilation. The code has been just interpreted
- 1~4: Deeper level of compilation has happened
-XX:+PrintCompilation Example



- As seen from the Figure 3, the PrimeNumbers::isPrime is marked with %, which means it is running in the most optimal way. And we can assume this is because the isPrime method is being called multiple times like 5000 times, which makes the JVM to optimize its way of running.

Leave a comment