Sub topic 1
Starting out: the bytecode interpreter
Every Java program on HotSpot begins its life in the same place. The JVM reads bytecode and executes it one instruction at a time, using a component called the bytecode interpreter. This chapter explains what that interpreter does, why HotSpot starts there, and why it cannot be where programs stay. The rest of the tutorial builds on this starting point.
Prerequisites
This chapter assumes you can do the following:
- Write and read basic Java: classes, interfaces, loops, and methods.
- Run
javacandjavafrom the command line. - Know that Java source compiles to bytecode, which the JVM then runs.
You do not need to know any JIT internals yet. Later chapters introduce the compilers, tiers, and flags.
What bytecode looks like
javac does not produce machine code. It produces .class files containing bytecode, a compact instruction set for an abstract stack machine. Each instruction is usually one byte for the opcode, plus operands. Instructions push values onto an operand stack, pop them, compute, and store results in numbered local variable slots.
Consider this class:
public class Add {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(2, 3));
}
}Compile it and disassemble the add method with javap, the JDK's class file disassembler:
javac Add.java
javap -c -p AddThe output for add is:
static int add(int, int);
Code:
0: iload_0
1: iload_1
2: iadd
3: ireturnRead it as a small program:
iload_0pushes local variable 0 (a) onto the operand stack.iload_1pushes local variable 1 (b).iaddpops two ints and pushes their sum.ireturnpops the int on top of the stack and returns it to the caller.
The bytecode does not mention registers, CPU instructions, or memory addresses. That is deliberate. The same .class file runs on x86-64, AArch64, and any other platform with a JVM.
What the interpreter does
The interpreter is the part of HotSpot that executes this bytecode directly. Conceptually it is a loop:
- Fetch the opcode at the current bytecode index.
- Jump to the code that implements that opcode.
- Update the operand stack, locals, or heap as the opcode requires.
- Advance to the next bytecode and repeat.
HotSpot's implementation is more refined than a C switch statement. It uses a template interpreter. At JVM startup, HotSpot generates a small piece of native machine code (a template) for each bytecode, and it dispatches from one template to the next. This is much faster than a naive interpreter written as a big loop. It is still an interpreter, because every bytecode is looked up and dispatched each time it runs.
Each method call in the interpreter creates an interpreter frame on the thread's stack. The frame holds the local variables, the operand stack, and a pointer to the method's runtime data. Because the frame has a well-defined, simple layout, the JVM can inspect or rebuild it. Later chapters on on-stack replacement and deoptimization rely on this property.
Why the JVM starts in the interpreter
HotSpot could compile every method to native code before running it. It does not, for three reasons.
- Startup time. The interpreter needs no compilation step. A method can run the moment its class is loaded and verified. Most programs execute large amounts of code only once, such as framework setup, configuration parsing, and static initializers. Compiling that code would cost more than just interpreting it.
- Knowledge. The JVM does not know in advance which methods matter. A typical application has thousands of methods, but a small fraction of them consume most of the run time. Waiting lets the JVM find that fraction by measurement.
- Information gathering. While interpreting, HotSpot counts how often methods are invoked and how often loops jump backward. In tiered mode, which is the default, it also records facts about the program's behavior. These are covered in the chapters on hotness counters and profiling. An optimizing compiler can later use that data to make better decisions than a static compiler could.
So the interpreter does two jobs. It runs code immediately, and it observes the code to decide what to compile.
How slow is it?
Interpreted code is much slower than compiled code. Each bytecode costs a dispatch plus memory traffic to the operand stack, and nothing is optimized across instructions. The gap depends on the workload, but one to two orders of magnitude is common for tight numeric loops.
You can measure this yourself. HotSpot's -Xint flag turns the JIT compilers off, so every method runs in the interpreter for the life of the process.
public class InterpTimer {
static int square(int x) {
return x * x;
}
public static void main(String[] args) {
long start = System.nanoTime();
long sum = 0;
for (int i = 0; i < 50_000_000; i++) {
sum += square(i % 1000);
}
long ms = (System.nanoTime() - start) / 1_000_000;
System.out.println("sum=" + sum + " time=" + ms + " ms");
}
}Run it twice:
javac InterpTimer.java
java InterpTimer
java -Xint InterpTimerThe sum value is the same in both runs. The time is not. The default run typically finishes in well under a second, because HotSpot compiles square and the loop in main while the program runs. The -Xint run is typically many times slower. Your exact numbers will differ by machine, JDK build, and load, so compare the ratio rather than the absolute values.
The counterpart flag: -Xcomp
-Xint has an opposite, -Xcomp, which asks HotSpot to compile methods on first use instead of interpreting them first. It is mainly a testing and debugging tool.
java -Xcomp InterpTimerYou might expect this to be fastest, but it often is not for whole programs. Startup becomes slow because everything gets compiled, including code that runs once. The compiler also has no profile data from interpretation, so it cannot make the speculative optimizations that make C2 code fast. This is direct evidence for the design: interpreting first and compiling later beats compiling everything immediately.
| Mode | Flag | Behavior | Typical use |
|---|---|---|---|
| Default | none | Interpret first, compile hot code in the background | Production |
| Interpreter only | `-Xint` | Never compile | Comparing speed, debugging |
| Compile on first use | `-Xcomp` | Compile methods before they run | Compiler testing |
Edge cases and common misunderstandings
- The interpreter never goes away. Even in a long-running server, new classes load, rarely used code paths execute, and deoptimized code falls back to the interpreter. The interpreter is the safety net under compiled code.
- Bytecode is not the "slow" part by itself. The bytecode format is fine. What costs time is executing it one instruction at a time. Compiled code is produced from the same bytecode.
- Class loading and verification are separate costs. A program's startup time includes loading and verifying classes, which is not interpretation. Do not attribute all slow startup to the interpreter.
- Native methods are not interpreted. A
nativemethod such asSystem.arraycopyruns machine code from the start. The interpreter only handles bytecode. -Xintchanges behavior, not meaning. A correct program gives the same results under-Xint. Only performance differs. If a program produces different results with and without the JIT, suspect a race condition in the program or a JVM bug.- Very small programs may never leave the interpreter. A command-line tool that runs for 200 ms may execute almost entirely as interpreted bytecode, and that is the intended outcome.
What comes next
The next chapter asks why HotSpot compiles at run time at all, given that the interpreter already works. The answer builds on the cost you just measured. After that, you will meet the two compilers, C1 and C2, and the counters that decide when a method moves from the interpreter to compiled code.
Exercise
You run a program normally and then again with java -Xint. Both runs print the same result, but the second takes ten times longer. What does this tell you about the interpreter, and why does HotSpot start in it anyway?
Show answer
It shows that interpreted execution is much slower than compiled execution, because each bytecode is fetched and dispatched individually with no cross-instruction optimization. In the normal run, HotSpot compiled the hot methods while the program ran. HotSpot still starts in the interpreter because it needs no compilation step, so code that runs only once costs nothing extra. It also lets the JVM count invocations and loop iterations, and gather profile data, to find which methods are worth compiling.
Exercise
Using javap -c -p, what would you expect the bytecode for static int mul(int a, int b) { return a * b; } to look like, and which two opcodes differ from the add example?
Show answer
The listing is iload_0, iload_1, imul, ireturn. Only the arithmetic opcode changes, from iadd to imul. The loads and return are identical because the method still takes two int locals and returns an int.