JIT optimization – Can we have mix of machine code and byte code
At runtime, for the same class, the JVM can execute some methods as machine code and others as bytecode.
🧠 How it works:
The JVM (including GraalVM) treats each method independently:
- If a method becomes “hot” (called frequently), the JIT compiler compiles it to machine code.
- If a method is rarely called, it remains in interpreted mode (bytecode).
- The JVM can call between them seamlessly.
💡 So yes — for the same class, some methods will run as compiled native code, others will still run as interpreted bytecode.
📦 Example
javaCopyEditpublic class MixedMode {
public static int hotMethod(int x) {
return x * 2;
}
public static int coldMethod(int x) {
return x * 1000000;
}
public static void main(String[] args) {
for (int i = 0; i < 1_000_000; i++) {
hotMethod(i); // gets compiled to machine code
}
coldMethod(42); // stays interpreted
}
}
Runtime Execution:
hotMethod()
becomes machine code after it hits a threshold (e.g., 10,000 invocations).coldMethod()
is executed as bytecode, since it’s only called once.