Sub topic 13
Compile Queues and Background Compiler Threads
This chapter assumes you know that Java source is compiled to bytecode, that the JVM runs that bytecode, and that methods and loops can become hot. You should also be comfortable running java from a command line.
Why compilation happens in the background
HotSpot does not normally stop all application work whenever it wants to compile a method. Instead, the running application records hot methods and submits compilation requests to an internal compile queue.
A compile request describes work such as:
- The method to compile.
- The compilation level requested, such as C1 level 3 or C2 level 4.
- The bytecode entry point.
- Whether the request is for normal method entry or on-stack replacement (OSR) inside a loop.
The application thread can continue running interpreted bytecode, or older compiled code, while the request waits in the queue.
HotSpot's CompileBroker coordinates this process. It accepts requests from the runtime, places them in compiler queues, and assigns them to compiler threads.
C1 and C2 compiler threads
Tiered compilation commonly has queues and worker threads for both compilers:
| Compiler | Main role | Typical queue wait |
|---|---|---|
| C1 | Quickly produces moderately optimized code and useful profiling | Usually shorter |
| C2 | Performs expensive, highly optimized compilation | Usually longer |
The exact number of threads depends on the JVM and machine. The CICompilerCount flag can control the number of compiler threads. More threads can reduce compilation delay, but they also consume CPU and memory. On a busy machine, too many compiler threads can compete with application threads.
While a request waits, the method is not frozen. It can execute in the interpreter or at its current compilation level. When compilation finishes, HotSpot safely changes future execution to the new native entry point.
A loop may generate a separate OSR request. This lets HotSpot replace an interpreted loop while that loop is already running. The method's normal entry point and its loop entry point can therefore be compiled at different times.
Observing queue activity
Compile output shows when requests finish compiling, rather than providing a complete queue trace. Run a program with:
javac HotLoop.java
java -XX:+PrintCompilation HotLoopA line can include a method name, a compilation identifier, and a tier level. The % marker commonly identifies an OSR compilation. A method shown at level 3 is using C1, while level 4 normally indicates C2.
For more detailed tiered events, use:
java -XX:+PrintCompilation -XX:+PrintTieredEvents HotLoopThe output can be very large. Use it to answer focused questions, such as whether a method reached C2 or whether an OSR compilation occurred.
Queue pressure and performance
Compilation is asynchronous, but it is not free. A workload that creates many hot methods can fill compiler queues. The effects include:
- A hot method waits longer before receiving optimized code.
- Compiler threads consume CPU while the application is active.
- Several versions of code may temporarily occupy the code cache.
- A method may be recompiled after new profiling information changes the best optimization.
Do not assume that the first call to a method measures its steady-state speed. The first calls may include interpretation, queue waiting, and compilation. Measure only after the application has reached the execution state you want to study.
Exercise
Why can an application continue running after HotSpot decides that a method should be compiled?
Show answer
HotSpot places the compilation request in an internal queue. A background C1 or C2 compiler thread processes it while the application continues using interpreted code or previously compiled code.