☕🧠 JAVA C1 vs C2: where does the JVM put your COMPILED CODE?
☕🧠 JAVA C1 vs C2: where does the JVM put your COMPILED CODE?
Your Java bytecode does not stay bytecode forever. 🔥
🔸 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
static int square(int x) { return x * x; } public static void main(String[] args) { for (int i = 0; i < 10_000_000; i++) { square(i); } }
At startup, square() exists as JVM bytecode.
The JVM can initially interpret it.
But after enough executions, HotSpot notices:
"This method is HOT." 🔥
#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.
Java bytecode
↓
C1
↓
Native machine code#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
Bytecode ↓ Interpreter ↓ C1 compiled code ↓ C2 highly optimized code
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
Java Heap ├── Objects ├── Arrays └── Application data
The generated machine code goes into native JVM memory called the:
👉 CODE CACHE
Conceptually:
JVM Process Memory
│
├── Java Heap
│ └── Java objects
│
├── Metaspace
│ └── Class metadata
│
├── Thread Stacks
│ └── Frames / local variables
│
└── Code Cache
└── JIT compiled native code
├── C1 code
└── C2 codeSo intensive JIT compilation consumes JVM process memory without necessarily increasing your heap.
#Memory #CodeCache
🔸 5️⃣ WATCH IT HAPPEN
Run:
java -XX:+PrintCompilation Main
You may see output similar to:
35 5 3 Main::square (4 bytes) 40 6 4 Main::square (4 bytes)
The compilation level is especially interesting.
With tiered compilation, HotSpot commonly uses levels:
0 → interpreted 1 → C1 2 → C1 3 → C1 + profiling 4 → C2
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.
The JVM does not simply "run Java code." It watches your application, learns what is hot, and recompiles it accordingly. ☕🔥
#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👇