Return to site

☕🧠 JAVA C1 vs C2: where does the JVM put your COMPILED CODE?

Your Java bytecode does not stay bytecode forever. 🔥

November 5, 2026

🔸 TLDR

HotSpot can progressively transform frequently executed Java bytecode into native machine code:

▪️ Interpreter → immediate execution

▪️ C1 → fast compilation

▪️ C2 → highly optimized compilation

▪️ Generated native code → stored in the Code Cache

And no: C1/C2 compiled code does NOT live in the Java heap.

🔸 1️⃣ START WITH BYTECODE

At startup, square() exists as JVM bytecode.

The JVM can initially interpret it.

But after enough executions, HotSpot notices:

#Java #JVM

🔸 2️⃣ C1: COMPILE FAST

C1 is the Client Compiler.

Its goal is roughly:

▪️ Compile quickly

▪️ Improve performance early

▪️ Collect profiling information

▪️ Avoid spending too much CPU optimizing

So the JVM can quickly replace interpreted execution with native instructions.

#JIT #HotSpot

🔸 3️⃣ C2: OPTIMIZE HARDER

If a method remains hot, the JVM may eventually use C2.

C2 spends more compilation time looking for optimizations such as:

▪️ Method inlining

▪️ Dead-code elimination

▪️ Escape analysis

▪️ Loop optimizations

▪️ Speculative optimizations

This is one reason why a Java application can become faster after warming up. 🚀

#Performance #JavaPerformance

🔸 4️⃣ BUT WHERE IS THAT CODE STORED? 🧠

Not here:

Java Heap

The generated machine code goes into native JVM memory called the:

👉 CODE CACHE

Conceptually:

So intensive JIT compilation consumes JVM process memory without necessarily increasing your heap.

#Memory #CodeCache

🔸 5️⃣ WATCH IT HAPPEN

Run:

You may see output similar to:

The compilation level is especially interesting.

With tiered compilation, HotSpot commonly uses levels:

So seeing level 4 means the method reached the JVM's highly optimized compilation tier. 🔥

🔸 TAKEAWAYS

▪️ C1 and C2 are JIT compilers, not memory regions.

▪️ C1 favors fast compilation and profiling.

▪️ C2 spends more effort producing highly optimized native code.

▪️ Hot methods can move progressively through compilation tiers.

▪️ JIT-generated machine code is stored in the Code Cache.

▪️ The Code Cache belongs to JVM native/process memory, not the Java heap.

#java #jvm #jit #hotspot #c1 #c2 #javaperformance #performance #memory #codecache #java27 #javadeveloper #softwareengineering #javacertification #ocpjava

🔸 SOURCES & FURTHER READING 📚

Want to verify how C1, C2, Tiered Compilation and the Code Cache really work inside HotSpot? ☕⚙️

▪️ Oracle: Compilation Optimization: C1 & C2 JIT Compilers https://docs.oracle.com/javacomponents/jrockit-hotspot/migration-guide/comp-opt.htm

▪️ Oracle: HotSpot Performance Enhancements: Tiered Compilation & Code Cache https://docs.oracle.com/en/java/javase/24/vm/java-hotspot-virtual-machine-performance-enhancements.html

Straight from the JVM documentation. 🔎☕

#Java #JVM #HotSpot #JIT #C1 #C2 #JavaPerformance #CodeCache

Go further with Java certification:

Java👇

Spring👇

SpringBook👇

JavaFullstackBook👇