Unlocking Java Performance: How Graal Replaces C2 in HotSpot JIT

This article explains Java's traditional JIT compilers, introduces Graal as a modular Java‑level JIT and AOT compiler, compares its optimizations with HotSpot's C2, and provides practical steps to enable Graal in Java 10, offering deeper insight into JVM performance tuning.

21CTO
21CTO
21CTO
Unlocking Java Performance: How Graal Replaces C2 in HotSpot JIT

Introduction

For most application developers the Java compiler is the javac command that produces .class files containing Java bytecode. This bytecode cannot run directly; it is interpreted by the JVM or compiled at runtime by a JIT compiler. HotSpot includes two C++‑implemented JIT compilers, C1 and C2.

Graal is a Java‑level JIT compiler that targets Java bytecode. Compared with C1 and C2 it is more modular and easier to maintain. Graal can operate as a dynamic JIT compiler or as a static AOT compiler. In Java 10 Graal was released experimentally as JIT compiler (JEP 317).

Tiered Compilation

HotSpot originally offered two JIT compilers, C1 (fast compilation, modest optimizations) and C2 (slow compilation, aggressive optimizations). Java 7 introduced tiered compilation, combining the strengths of both and the interpreter into five levels:

level 0: interpreter

level 1: C1 without profiling

level 2: C1 with method and loop back‑edge profiling

level 3: C1 with additional branch and receiver‑type profiling

level 4: C2 (final compilation)

Typically a method starts at level 0, progresses to level 3 (C1), and finally to level 4 (C2) once sufficient profiling data is collected.

Graal vs C2

Because a JIT compiler is a source‑to‑target code transformer, optimizations implemented in C2 can be ported to Graal and vice‑versa. Many C2 optimizations, such as the String.compareTo intrinsic, have already been migrated. However, some Graal‑specific optimizations like its inlining algorithm and partial escape analysis (PEA) have not been back‑ported to C2.

Inlining replaces call‑site method bodies with their implementations, enabling further optimizations. Graal provides two inliners: a community version that uses depth‑first search and an enterprise version that ranks call‑sites by size and potential gain.

Escape analysis (EA) determines whether objects can be allocated on the stack. Graal’s partial escape analysis adds control‑flow information, allowing it to virtualize heap allocations on non‑escaping branches, achieving higher scalar‑replacement rates than C2.

Using Graal

On Java 10 (Linux/macOS) the default HotSpot compiler is still C2. Adding -XX:+UnlockExperimentalVMOptions -XX:+UseJVMCICompiler to the java command replaces C2 with Graal.

Oracle Labs GraalVM is a JDK distribution based on Java 8 that includes Graal Enterprise. The source code is available on GitHub; building it requires the mx tool and labsjdk.

Project setup scripts such as mx eclipseinit, mx intellijinit, or mx netbeansinit generate IDE configuration files.

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.

JavacompilerJITHotSpotgraalvm
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.