Unlock Java Performance: How JIT and Tiered Compilation Supercharge Your Apps

This article explains how Java's Just‑In‑Time (JIT) compilation and tiered (layered) compilation work together to transform bytecode into native code, improve runtime performance, balance startup speed, and offers practical configuration tips for fine‑tuning JVM behavior.

21CTO
21CTO
21CTO
Unlock Java Performance: How JIT and Tiered Compilation Supercharge Your Apps

JIT compilation in Java improves performance by converting bytecode to native code at runtime, optimizing execution while balancing startup speed.

Unlike ahead‑of‑time (AOT) compilation, which happens during build time, Java first generates bytecode and then the JVM translates it to native machine code during execution, adhering to the "write once, run anywhere" principle.

The JVM identifies frequently executed code (hot spots) and compiles it with a JIT compiler; the term "HotSpot VM" originates from this behavior.

JIT is also used in .NET, JavaScript V8, Python, and PHP environments, but this article focuses on Java.

JVM JIT

At runtime the JVM loads bytecode, interprets it, and executes it, which is slower than native code. JIT compiles bytecode to native code on the fly and caches the compiled code for reuse.

Because compiling every method at startup would hurt launch time, the JVM initially compiles only frequently called methods and later compiles less‑used methods based on actual usage.

The JVM maintains a method‑call counter; each invocation decrements the counter, and when it reaches zero the JIT compiles the method.

Another counter tracks loop back‑edges; if the threshold is exceeded, JIT compilation may be skipped for that loop.

About JIT Compilers

C1 – client compiler → low compilation threshold, optimized for fast startup.

C2 – server compiler → higher threshold, richer profiling information, produces highly optimized code for long‑running workloads.

Tiered (Layered) Compilation

JIT can compile code at different optimization levels based on usage and complexity. Higher levels yield better performance but consume more resources.

Tiered Compilation Levels

Level 0 – interpret code → no optimization, used at startup to gather profiling data.

Level 1 – simple C1 compiled code → low‑complexity methods are compiled without further profiling.

Level 2 – limited C1 compiled code → a few frequently used methods receive early compilation based on limited profiling.

Level 3 – full C1 compiled code → all important hot methods are compiled with complete profiling information.

Level 4 – C2 compiled code → maximum optimization using all available profiling data, ideal for long‑running code.

Methods may be re‑compiled at higher levels if the JIT decides further optimization is beneficial.

Optimization Strategies

Sometimes optimized methods become unsuitable; the compiler may revert to a lower optimization level or back to interpretation. Monitoring such switches and adjusting source code can reduce overhead.

Configuration

JIT and tiered compilation are enabled by default. To disable JIT, start the JVM with -Djava.compiler=NONE or -Xint. To disable tiered compilation, use -XX:-TieredCompilation. To limit compilation to C1 only, set -XX:TieredStopAtLevel=1. Thresholds for levels 2‑4 can be adjusted, for example:

-XX:TierYCompileThreshold=0
-XX:TierYInvocationThreshold=100
-XX:TierYMinInvocationThreshold=50
-XX:TierYBackEdgeThreshold=1500

Any configuration change can affect performance; thorough benchmarking is recommended before applying tweaks.

JDK HotSpot vs. GraalVM JIT

GraalVM is built on HotSpot but replaces the traditional C2 compiler with the Graal compiler written in Java, offering advanced optimizations such as improved inlining, partial escape analysis, and speculative optimizations. GraalVM also supports multiple languages and provides AOT compilation for faster startup and lower memory usage.

HotSpot remains a proven, stable Java runtime, while GraalVM pushes performance and flexibility boundaries; the choice depends on workload characteristics and interoperability needs.

Conclusion

JIT compilation transforms bytecode into native code at runtime, optimizing frequently used methods while balancing startup speed. Tiered compilation incrementally refines code based on profiling data, delivering further performance gains. Default JIT settings work well for most applications, but careful tuning can yield significant efficiency improvements for high‑performance scenarios.

Author: Luo Yi References: https://en.wikipedia.org/wiki/Just-in-time_compilation https://docs.oracle.com/en/java/javase/21/vm/java-hotspot-virtual-machine-performance-enhancements.html https://developers.redhat.com/articles/2023/09/29/how-we-solved-hotspot-performance-puzzle#problem_and_solution
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JVMJITHotSpotgraalvmTiered Compilation
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.