C1 and C2 compiler threads are created by a Java digital machine to optimize your software’s efficiency. Sometimes these threads will are likely to devour excessive CPU. On this publish, let’s be taught somewhat extra about C1 and C2 compiler threads and how one can deal with their excessive CPU consumption.
After studying this publish, terminologies like Hotspot JIT, C1 compiler threads, C2 compiler threads, and code cache might not terrify you (because it used to terrify me up to now).
What Is HotSpot JIT Compiler?
Your software might have thousands and thousands of strains of code. Nevertheless, solely a small subset of code will get executed many times. This small subset of code (often known as “HotSpot”) is liable for your software efficiency. At runtime, JVM makes use of this JIT (just-in-time) compiler to optimize this HotSpot code. More often than not, code written by the applying builders will not be optimum. Thus, JVM’s JIT compiler optimizes the developer’s code for higher efficiency. To do that optimization, the JIT compiler makes use of C1 and C2 compiler threads.
What Is Code Cache?
The reminiscence space that the JIT compiler makes use of for this code compilation is named “code cache.” This space resides exterior of the JVM heap and metaspace. To study completely different JVM reminiscence areas, it’s possible you’ll seek advice from this video clip.
What Is the Distinction Between C1 and C2 Compiler Threads?
In the course of the early days of Java, there have been two varieties of JIT compilers:
- Shopper
- Server
Primarily based on what kind of JIT compiler you need to use, acceptable JDKs need to be downloaded and put in. If you’re constructing a desktop software, then JDK with a “consumer” JIT compiler must be downloaded. If you’re constructing a server software, then JDK that has a “server” JIT compiler must be downloaded.
Shopper JIT compiler begins compiling the code as quickly as the applying begins. Server JIT compiler will observe the code execution for fairly a while. Primarily based on the execution data it positive aspects, it is going to begin doing the JIT compilation. Though server JIT compilation is sluggish, the code it produces might be much more superior and performant than the one produced by the consumer JIT compiler.
In the present day trendy JDKs are shipped with each consumer and server JIT compilers. Each of the compilers attempt to optimize the applying code. In the course of the software startup time, code is compiled utilizing the consumer JIT compiler. Later as extra data is gained, code is compiled utilizing the server JIT compiler. That is known as tiered compilation in JVM.
JDK builders had been calling them consumer and server JIT compilers, internally as C1 and C2 compilers. Thus, the threads utilized by the consumer JIT compiler are known as C1 compiler threads. Threads utilized by the server JIT compiler are known as C2 compiler threads.
C1, C2 Compiler Threads Default Dimension
The default variety of C1 and C2 compiler threads are decided primarily based on the variety of CPUs which might be accessible on the container/system during which your software is operating. Right here is the desk which summarizes the default variety of C1 and C2 compiler threads:
Default C1, C2 compiler thread rely
You’ll be able to change the compiler thread rely by passing -XX:CICompilerCount=N
JVM argument to your software. One-third of the rely you specify in -XX:CICompilerCount
might be allotted to the C1 compiler threads. The remaining thread rely might be allotted to C2 compiler threads. Suppose you’ll 6 threads (i.e., -XX:CICompilerCount=6
), then 2 threads might be allotted to C1 compiler threads and 4 threads might be allotted to C2 compiler threads.
C1, C2 Compiler Thread Excessive CPU Consumption: Potential Options
Typically you would possibly see C1 or C2 compiler threads devour a excessive quantity of CPU. When any such drawback surfaces, under are the potential answer to handle it:
1. Do Nothing (If Intermittent)
In your case, in case your C2 compiler thread’s CPU consumption is barely intermittently excessive and never repeatedly excessive, and it doesn’t damage your software’s efficiency, then you may think about ignoring the issue.
2. -XX:-TieredCompilation
Go this -XX:-TieredCompilation
JVM argument to your software. This argument will disable the JIT HotSpot compilation. Thus CPU consumption will go down. Nevertheless, as a side-effect, your software’s efficiency can degrade.
3. -XX:TieredStopAtLevel=N
If a CPU spike is prompted due to C2 compiler threads alone, you may flip off C2 compilation alone. You’ll be able to move -XX:TieredStopAtLevel=3
. If you move this -XX:TieredStopAtLevel
argument with worth 3, then solely C1 compilation might be enabled and C2 compilation might be disabled.
There are 4 tiers of compilations:
If you say -XX:TieredStopAtLevel=3
, then code might be compiled solely as much as “Full C1 compiled code” degree. C2 compilation might be stopped.
4. -XX:+PrintCompilation
You’ll be able to move -XX:+PrintCompilation
JVM argument to your software. It can print particulars about your software’s compilation course of. It can facilitate you to tune the compilation course of additional.
5. -XX:ReservedCodeCacheSize=N
The code that the Hotspot JIT compiler compiles/optimizes is saved within the code cache space of the JVM reminiscence. The default measurement of this code cache space is 240MB. You’ll be able to enhance it by passing -XX:ReservedCodeCacheSize=N
to your software. To illustrate you need to make it 512 MB. You’ll be able to specify it like this: -XX:ReservedCodeCacheSize=512m
. Rising the code cache measurement has the potential to cut back the CPU consumption of the compiler threads.
6. -XX:CICompilerCount
You’ll be able to think about rising the C2 compiler threads through the use of the argument -XX:CICompilerCount
. You’ll be able to seize the thread dump and add it to instruments like fastThread.There you may see the variety of C2 compiler threads. If you happen to see a fewer variety of C2 compiler threads and you’ve got extra CPU processors/cores, you may enhance the C2 compiler thread rely by specifying -XX:CICompilerCount=8
argument.
Video