JIT tuning
- Java course
- Expert
- Lesson 35/46
- 5 min
Learn JIT flags and tiered compilation settings.
JIT compilation
The JIT (Just-In-Time) compiler turns frequently called hot methods into machine code. HotSpot uses two compilers: C1 compiles fast (little optimisation), C2 does aggressive optimisation. Tiered compilation (-XX:+TieredCompilation) uses C1 first, then C2.
The compile threshold is set with -XX:CompileThreshold; a method is compiled once it is called this many times. -XX:+PrintCompilation shows what gets compiled.
JIT tuning
JIT tuning is rarely needed; the default tiered compilation is good for most workloads. Still, -XX:+PrintCompilation shows what is compiled and -XX:+UnlockDiagnosticVMOptions reveals detailed flags.
Premature tuning should be avoided; first find the bottleneck with JFR and JMH. The aggressive optimisation of C2 sometimes gives surprising speedups; that is why microbenchmarking uses JMH.
Practice Write the JitConfig class
Define class JitConfig with a static String describe(int threshold) method that returns "CompileThreshold=" + threshold + " Tiered=true". In main print System.out.println(describe(10000));.
Starter code
// Define the JitConfig class: describe method builds the JIT config string
// main: print the config for threshold 10000Sample solution
class JitConfig {
static String describe(int threshold) {
return "CompileThreshold=" + threshold + " Tiered=true";
}
public static void main(String[] args) {
System.out.println(describe(10000));
}
}Expected output
CompileThreshold=10000 Tiered=true
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Complete the tiered compilation flag
-XX:+____ -XX:CompileThreshold=10000
-XX:+TieredCompilation -XX:CompileThreshold=10000
Explanation: -XX:+TieredCompilation uses C1 and C2 together.
2 Match the JIT concepts
| Term | Meaning |
|---|---|
C1 |
Fast compiler |
C2 |
Optimising compiler |
CompileThreshold |
Compile threshold |
TieredCompilation |
Tiered compilation |
3 What does the JIT do?
- It compiles source code
- It creates threads
- It collects garbage
- It turns hot methods into machine code Correct answer
Explanation: The JIT compiles frequently called methods into machine code.