Optimizing JVM Threads
Principles
During the running of service processes, the JVM starts the recycling thread to perform operations such as JIT compilation and heap space management and garbage collection. The number of JVM running threads directly affects the system performance. Find the JVM thread with high CPU usage through resource monitoring and perform tuning on the thread to optimize the running performance of service programs.
Use the following method to find the JVM thread with high CPU usage:
- Run the top command to find the Java process ID. In this example, the process ID is 16498.

- Run the top -H -p <pid> command to trace the CPU usage of each thread of the process. The following figure uses thread 16810 as an example.

- Convert the thread ID to a hexadecimal value using the calculator in Windows. For example, 16810 is converted to 41aa.
- Run the jstack command to query the thread stack information.
1jstack -l <process ID> | grep <hexadecimal thread ID>

The preceding information indicates that the thread name is cronJobScheduler_Worker-8, which is not a JVM thread. In the following figure, Gang worker#0 (Parallel GC Threads), Concurrent Mark-Sweep GC Thread and Gang worker#1 (Parallel CMS Threads) are JVM threads.

Modification Method
- Method 1:
- If the CPU usage of the GC thread is high and the system CPU resources are sufficient, you can increase the number of GC threads to reduce the pressure of the GC thread. The GC method parameters vary depending on the GC method. The CMS uses the -XX:ParallelCMSThreads parameter, and the Parallel uses the -XX:ParallelGCThreads parameter.
The following is an example of setting the number of GC threads of the CMS to 2:
1# /path/java -XX:ParallelCMSThreads=2 <other parameters> - Generally, the threads whose names contain C1 or C2 are JIT threads. If these threads occupy a large number of CPU resources, you can increase the value of -XX:CompileThreshold to reduce the frequency of triggering JIT. In C2 mode, the default value of this parameter is 10000. In C1 mode, the default value of this parameter is 1500. To set the JIT threshold to 20000, run the following command:
1# /path/java -XX:CompileThreshold=20000 <other parameters>
- If the CPU usage of the GC thread is high and the system CPU resources are sufficient, you can increase the number of GC threads to reduce the pressure of the GC thread. The GC method parameters vary depending on the GC method. The CMS uses the -XX:ParallelCMSThreads parameter, and the Parallel uses the -XX:ParallelGCThreads parameter.
- Method 2:
Adjust the number of compilation threads by setting -XX:+CICompilerCountPerCPU to true. The number of compilation threads is automatically configured based on the number of processor cores. Set -XX:+CICompilerCountPerCPU to false and -XX:+CICompilerCount to N to forcibly set the total number of compilation threads to N. For example, to set the number of JIT threads to 4:
1# /path/java -XX:+CICompilerCountPerCPU=false -XX:+CICompilerCount=4 <other parameters> - Method 3:
You can disable the JIT by setting -XX:+CICompilerCount to 0.
- If the number of threads is increased, the CPU usage increases, and the number of context switching times increases, which may cause service performance deterioration. Therefore, you need to adjust the parameters based on the actual service situation.
- Disabling the JIT may affect service performance.