Lesson 4 of 8
JIT Compilation & Performance
The JIT (Just-In-Time) compiler is what makes Java fast. It compiles frequently executed bytecode ('hot spots') into native machine code at runtime. Understanding JIT enables you to write code that compiles efficiently and avoid patterns that prevent optimisation.
How JIT Works & Enabling Optimisations
// JIT Compilation Tiers:
// Tier 0: Interpreter (initial execution - slow, no profiling)
// Tier 1-3: Client compiler (C1) - fast compilation, basic optimisations
// Tier 4: Server compiler (C2) - deep optimisation (inlining, loop unrolling, escape analysis)
// Java 17+ uses Graal JIT as optional replacement with better optimisations
// Key JIT optimisations:
// 1. Method inlining - small method body copied into caller (eliminates call overhead)
// 2. Escape analysis - object that doesn't 'escape' a method allocated on STACK (not heap)
// → No GC overhead! No synchronisation needed for unreachable objects
// 3. Loop unrolling - expand loop body to reduce branch overhead
// 4. Dead code elimination - remove code that can never execute
// 5. Branch prediction - JIT optimises hot branches
// Patterns that HURT JIT:
// ❌ Very large methods - harder to inline (JVM has -XX:MaxInlineSize limit)
// ❌ Megamorphic call sites - calling the same interface method with >2 different impl types
// at the same call site prevents virtual dispatch optimisation
// ❌ String concat in tight loops - (use StringBuilder / StringJoiner)
// ❌ Excessive boxing - Integer, Double in collections vs primitives in arrays
// Measuring JIT impact
public class JITDemo {
// Simple example: JIT inlines and optimises this heavily-called method
private static int square(int n) { return n * n; } // tiny → always inlined
public static void main(String[] args) {
// Warmup - let JIT compile hot paths
long sum = 0;
for (int i = 0; i < 1_000_000; i++) {
sum += square(i); // after ~10k iterations, JIT compiles square() and inlines it
}
// After warmup, this loop is native-speed:
long start = System.nanoTime();
sum = 0;
for (int i = 0; i < 10_000_000; i++) sum += square(i);
long ns = System.nanoTime() - start;
System.out.printf("Sum=%d in %d ms%n", sum, ns / 1_000_000);
// JVM flags for JIT insight:
// -XX:+PrintCompilation See what gets JIT-compiled
// -XX:+PrintInlining See inline decisions
// -XX:CICompilerCount=N Number of JIT compiler threads
// -Xcomp Force JIT all methods (skip interpreter tier)
// -Xint Interpreter only (disable JIT - for benchmarking)
}
}Quick Quiz
Tip
Tip
Practice JIT Compilation Performance in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for JIT Compilation & Performance
Practice Task
Note
Practice Task - (1) Write a working example of JIT Compilation Performance from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with JIT Compilation Performance is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.
Finished this lesson?