Sub topic 13
Compile queues and background compiler threads
This chapter covers how HotSpot schedules compilation work. It assumes you know the interpreter, the C1 and C2 compilers, the five tiers, and the invocation and back-edge counters from earlier chapters. It also assumes you can read -XX:+PrintCompilation output.
Compilation does not stop your program
When a counter crosses a threshold, the thread running the method does not compile it. It creates a compilation task and puts it in a queue. Then it keeps running the method in the interpreter, or in the current compiled version, as if nothing had happened. Separate compiler threads take tasks from the queue and produce native code. When a task finishes, HotSpot installs the new code and the next call to the method uses it.
This design has three consequences:
- Compilation costs CPU time on other threads, but it does not pause your application threads. A compile that takes 200 ms does not freeze the program for 200 ms.
- Between the trigger and the installation, the method runs at its old speed. Your program experiences this delay as warmup.
- Compilation capacity is limited. If the queues grow faster than the compiler threads can drain them, hot code waits longer to be optimized.
Two queues, two thread pools
With tiered compilation, HotSpot keeps one queue for C1 and one for C2. Each queue has its own compiler threads. A C1 task is cheap and usually finishes in milliseconds. A C2 task can take much longer, especially for large methods with a lot of inlining. Separate pools prevent slow C2 work from blocking quick C1 work. A method that needs a tier 3 compile gets one soon, even while C2 is busy on something large.
The total number of compiler threads is -XX:CICompilerCount. With tiered compilation HotSpot gives about one third of the threads to C1 and the rest to C2, with at least one for each. On a machine with 4 CPUs the default count is typically 3: one C1 thread and two C2 threads. The default grows slowly as CPU count rises, and a container with a CPU limit may get a much smaller number than the host has. Tiered compilation needs at least 2 threads, so -XX:CICompilerCount=1 is rejected.
Compiler threads are visible as C1 CompilerThread0, C2 CompilerThread0, and so on, in thread dumps and in OS tools such as top -H. They run in the JVM, and they use native memory, not Java heap, for the data structures they build during a compile.
Dynamic thread counts
In JDK 11, -XX:+UseDynamicNumberOfCompilerThreads is on by default. HotSpot reserves the maximum number of threads but starts only a few at first. It adds threads when queues grow, and it lets idle threads exit. So a thread dump of a quiet application may show fewer compiler threads than CICompilerCount suggests.
You can check the values on your machine:
java -XX:+PrintFlagsFinal -version | grep -E 'CICompilerCount|BackgroundCompilation|UseDynamicNumberOfCompilerThreads|TieredCompilation 'The output lists each flag with its current value. CICompilerCount shows the default for your CPU count.
How a task is chosen from the queue
The queue is not first-in, first-out. With tiered compilation, HotSpot picks the task whose method is currently being used most heavily, measured as the rate of invocation and loop events. A method that was hot a moment ago but has gone quiet loses priority. HotSpot also drops tasks for methods that have been unused for a while, and for methods whose classes were unloaded. This keeps the queue focused on code that matters now, not code that mattered at the start of the program.
Load feedback: thresholds respond to queue length
HotSpot also adjusts its own thresholds based on how long the queues are. Two groups of flags control this:
| Flag | Default | Effect |
|---|---|---|
| `Tier3LoadFeedback` | 5 | The tier 3 thresholds scale up as the C1 queue grows relative to the number of C1 threads. A longer queue means methods need more invocations to qualify. |
| `Tier4LoadFeedback` | 3 | The same scaling for tier 4, using the C2 queue. |
| `Tier3DelayOn` | 5 | If the C2 queue is longer than this value per C2 thread, new methods go to tier 2 instead of tier 3. Tier 2 code is faster to produce and has less profiling overhead. |
| `Tier3DelayOff` | 2 | When the C2 queue falls below this value per thread, methods return to the normal tier 3 path. |
The effect is automatic backpressure. When C2 is behind, HotSpot avoids creating more work that profiled code would later feed to C2. When the backlog clears, normal behavior resumes. You rarely need to change these values, but knowing they exist helps you interpret warmup curves that differ between a 2-CPU container and a 16-CPU server.
Background compilation and `-Xbatch`
-XX:+BackgroundCompilation is the default. It is what makes the thread continue while its task waits. Turning it off, either with -XX:-BackgroundCompilation or with the shorthand -Xbatch, makes the requesting thread wait until the compile finishes.
This is a diagnostic tool. It makes runs more repeatable, because the timing of when compiled code appears no longer depends on thread scheduling. It also makes startup much slower. Do not use it in production.
You can see the difference using the HotLoop program from earlier in this tutorial:
javac HotLoop.java
# Default: compiles in the background
time java -XX:+PrintCompilation HotLoop > default.txt
# Compile in the foreground
time java -Xbatch -XX:+PrintCompilation HotLoop > batch.txtCompare the two output files. With -Xbatch, the order of lines is stable from run to run, because each compile finishes before the program moves on.
Inspecting the queues of a running JVM
The jcmd tool can ask a running JVM about its compiler state. Start any long-running Java process, find its process id with jcmd (no arguments lists JVMs), and run:
jcmd <pid> Compiler.queue
jcmd <pid> Thread.print | grep CompilerThreadCompiler.queue prints the tasks currently waiting in each queue. On a healthy, warmed-up application it is usually empty or very short. A long queue that stays long during steady-state operation is a sign that compilation capacity is too small for the workload. The second command lists the compiler threads that currently exist.
Common problems
- Slow warmup in small containers. With a CPU limit of 1 or 2, there are few compiler threads, and they compete with application threads for the same CPUs. Methods stay in the interpreter or at tier 3 longer. Giving the container more CPU often shortens warmup more than any JIT flag.
- Raising
CICompilerCountwithout more CPUs. More compiler threads on the same CPUs just take time from the application. Increase the count only if the machine has idle CPUs andCompiler.queueshows a persistent backlog. - Treating the first seconds as typical. Queue backlog is highest at startup, when many methods become hot at once. Measurements taken then reflect queue delay, not the speed of optimized code.
- Reading
PrintCompilationtimestamps as compile durations. The timestamp column shows when the event was printed, not how long the task waited in the queue or how long it took to compile.
Exercise
A service in a container limited to 1 CPU takes about 90 seconds to reach its normal request latency. On a 16-CPU host the same service takes 15 seconds. Using what you know about compile queues, give one likely reason, and say what you would check in a running instance.
Show answer
With 1 CPU, the compiler threads and application threads share one CPU, and HotSpot starts few compiler threads. Hot methods wait in the queues, and the tier 3 and tier 4 thresholds rise because of load feedback, so methods stay in slower code longer. To check, run jcmd <pid> Compiler.queue during the slow period to see whether tasks are waiting. Also check -XX:+PrintFlagsFinal for CICompilerCount inside the container. Giving the container more CPU is the most direct fix.
Exercise
What does -Xbatch change, and why is it unsuitable for production?
Show answer
It disables background compilation, so the thread that triggers a compile waits until the compile finishes. This makes compile timing repeatable, which helps diagnosis. In production it stalls application threads during every compile, which slows startup and adds latency.