Sub topic 9
Profiling: What the JVM Learns While Running
This chapter assumes basic Java programming, knowledge that Java source becomes bytecode, and comfort running javac and java from a command line.
Why the JVM profiles code
The HotSpot JVM starts by interpreting bytecode. While the program runs, it records information about how that bytecode behaves. This information is called a runtime profile.
The profile helps the JIT compiler answer questions such as:
- Which methods are called often?
- Which branches are usually taken?
- Which concrete class reaches an interface or superclass call?
- How often does a loop repeat?
- Which methods and callers appear together?
The profile is not a permanent description of the program. It describes the executions seen so far. A program that processes one kind of object during warmup may later receive another kind.
What gets recorded
HotSpot records several kinds of information. The exact counters and data structures are implementation details, but these categories are useful when reasoning about optimization.
| Profile information | Example | Possible compiler use |
|---|---|---|
| Invocation counts | `parse()` is called millions of times | Compile the method |
| Back-edge counts | A loop returns to its header often | Compile the loop with OSR |
| Type profiles | An interface call usually receives `Square` | Inline `Square.area()` |
| Branch behavior | A condition is true almost every time | Optimize the common path |
| Call-site behavior | A call usually targets one method | Treat it as effectively monomorphic |
A method call site with one observed receiver type is called monomorphic. A site with several observed types is polymorphic. A site with many types is megamorphic. In general, a single common type gives the compiler more useful specialization information.
A profiling example
The following program first sends only Square objects through Shape.area(). It then sends Circle objects through the same method.
public class ProfileDemo {
interface Shape {
double area();
}
static final class Square implements Shape {
public double area() {
return 4.0;
}
}
static final class Circle implements Shape {
public double area() {
return Math.PI;
}
}
static double totalArea(Shape shape, int count) {
double total = 0.0;
for (int i = 0; i < count; i++) {
total += shape.area();
}
return total;
}
public static void main(String[] args) {
double total = 0.0;
for (int i = 0; i < 5_000; i++) {
total += totalArea(new Square(), 10_000);
}
for (int i = 0; i < 5_000; i++) {
total += totalArea(new Circle(), 10_000);
}
System.out.println(total);
}
}During the first phase, profiling may show that shape.area() almost always targets Square.area(). C2 can use that observation to inline the method and simplify the call. When Circle appears, the old assumption may no longer be valid. HotSpot can retain a type check for the common case and provide another path for other types.
Profiling also records branch behavior. If a branch is nearly always taken, compiled code can place that path first. The less common path still needs to work because the profile is only a prediction.
From profile to optimized machine code
The usual relationship is:
Profiling does not directly make code faster. It supplies evidence that lets the compiler choose faster code for the common case. If that evidence becomes misleading, deoptimization lets the JVM return to an interpreter or less specialized compiled version.
Exercise
Exercise
Why can observing only Square objects at an interface call help C2 optimize the call, and why must the JVM still handle Circle objects later?
Show answer
The type profile identifies Square as the common receiver, so C2 may inline Square.area() and specialize the common path. The profile is only an observation, so the JVM must keep a safe path or deoptimize when Circle appears.