Sub topic 2
What the JVM does while your code runs
This chapter assumes you can write and run Java from the command line, know what threads and a heap are, and can read a stack trace. It builds the working model you need before you read any profile: the heap, the garbage collector (GC), and the JIT compiler all consume time that never appears in your source code.
Three things happen besides your logic
When you start a program, the JVM runs your bytecode and also does background work on your behalf.
- Heap management. Every object you create with
newlives on the heap, a memory region the JVM manages for you. - Garbage collection. The GC finds objects that nothing references and reclaims their memory.
- JIT compilation. The JIT (just-in-time) compiler turns bytecode that runs often into optimized machine code while the program is running.
Each of these can make a program slow without any line of your code being "wrong".
The heap and the garbage collector
Most collectors are generational. They rely on the observation that most objects die young. New objects go into a young generation, which is collected often and cheaply. Objects that survive several collections move to the old generation, which is collected less often and usually at a higher cost.
Allocation itself is cheap, often just advancing a pointer. The cost comes later, when the heap fills and a collection must run. Two numbers matter for performance:
- Allocation rate: how many bytes per second your code creates. A high rate makes collections frequent.
- Pause time: how long application threads stop while the GC works. Some phases stop all threads (a "stop-the-world" pause), and this shows up directly as latency spikes.
A program that allocates heavily can spend a large share of its time in GC, even though the GC never appears in your call stacks.
The JIT compiler and warmup
The JVM first interprets bytecode, which is slow. It counts how often each method runs. When a method becomes hot, the JIT compiles it to machine code and applies optimizations such as inlining small methods and removing unused work. Later it may recompile the method with more aggressive optimizations.
The consequence is warmup. A freshly started JVM runs slower than the same JVM a minute later. If you time a short run, you may be measuring the interpreter and the compiler instead of your algorithm.
Seeing it in a real program
The slow program below builds strings in a loop. Each s += i creates a new String, copies the old contents, and leaves the previous string as garbage.
public class Slow {
public static void main(String[] args) {
long start = System.nanoTime();
for (int round = 0; round < 200; round++) {
String s = "";
for (int i = 0; i < 5_000; i++) {
s += i;
}
if (s.length() == 0) {
System.out.println("unreachable");
}
}
System.out.println((System.nanoTime() - start) / 1_000_000 + " ms");
}
}This program shows all three effects. It allocates a large volume of short-lived strings, so the young generation fills quickly and the GC runs often. It also copies growing strings, which is real CPU work. The JIT compiles the loop early, so most of the run uses optimized code.
Common mistakes
- Treating a single short run as a measurement, which includes warmup.
- Assuming the GC is free because you never call it.
- Tuning code before checking whether GC pauses explain the slowness.
Exercise
A service handles requests quickly for most of the day but shows latency spikes every few seconds. CPU profiling shows no single hot method. Which part of the JVM would you check first, and why?
Show answer
Check the garbage collector. Periodic spikes with no hot method fit stop-the-world GC pauses caused by a high allocation rate, since GC work does not appear in your call stacks. GC logs would confirm it by showing pause times that line up with the spikes.