If fate is a lonely river, who will be your soul ferry? —— Claire McFall, "The Ferryman"
1. Problem
- the infinite loop of 1613de4c59fee7 while cause the CPU usage to soar?
- frequent Young GC cause a surge in CPU usage?
- Is the CPU usage high for applications with a large number of threads?
- there a large number of threads in applications with high CPU usage?
- in the BLOCKED state cause the CPU usage to soar?
- the CPU in the time-sharing operating system consume
us (user mode) or
sy (kernel mode)?
2. Thinking
1. How do we calculate CPU usage?
CPU% = (1 - idleTime / sysTime ) * 100
idleTime
: CPU idle timesysTime
: The sum of the time the CPU is in user mode and kernel mode
2. What are the common CPU-intensive operations?
It is often said that computationally intensive programs are CPU intensive. So, what operations in Java applications are computationally intensive?
Common CPU-intensive operations are listed below:
- Frequent GC; if the number of visits is large, it may cause frequent GC or even Full GC. When the call volume is large, the memory allocation will be so fast that the GC thread will continue to execute, causing the CPU to surge.
- Serialization and deserialization
- Encryption and decoding
- Regular expressions. The reason may be that the engine implementation used by Java regular expressions is the NFA automata, which will backtrack when the characters are matched.
- Thread context switch. There are many threads that are started, and the state of these threads is constantly changing between Blocked (lock waiting, IO waiting, etc.) and Running. This situation can easily happen when lock contention is intense.
- Some threads are performing non-blocking operations, such as the
while (true)
statement. If the calculation time in the program is longer, you can sleep the thread.
3. Is the CPU related to processes and threads?
Now, the time-sharing operating system uses polling to allocate time slices for process scheduling. If the process is waiting or blocking, it will not use CPU resources. Threads are called light processes and share process resources, so thread scheduling is also time-sharing in the CPU. But in Java, we use JVM for thread scheduling, so generally speaking, there are two modes of thread scheduling: time-sharing scheduling and preemptive scheduling. Threads and processes do not use CPU resources when they are blocked or waiting.
3. Answer
1. Does the infinite loop of while cause the CPU usage to soar?
answer : Yes
analysis : First, the infinite loop will call the CPU register to count, this operation will take up CPU resources. So, if the thread has been in an infinite loop, will the CPU switch threads? Unless the operating system time slice expires, the infinite loop will not give up the occupied CPU resources, and the infinite loop will continue to request the time slice from the system until the system has no free time to do other things.
2. Will frequent Young GC cause the CPU usage to soar?
answer : Yes
analyzes : Young GC itself is a garbage collection operation of JVM, which needs to calculate memory and call registers, so the frequent Young GC will definitely occupy CPU resources.
Let us look at a real case: the for loop queries the data collection from the database, and then encapsulates the new data collection again. If the memory is not enough for storage, the JVM will reclaim data that is no longer used. Therefore, if you need a lot of storage space, you may receive a CPU usage alert.
3. Is the CPU usage of applications with many threads necessarily high?
answer : Not necessarily
analysis : If we jstack
, the total number of threads is very large, but there are Runnable
and Running
states, so the CPU usage is not necessarily high. But in most cases, if the number of threads is large, the common cause is that a large number of threads are in the BLOCKED
and WAITING
states.
4. Does an application with high CPU usage necessarily have a large number of threads?
answer : Not necessarily
analysis : The key factor for high CPU usage is computationally intensive operations. If a thread has a lot of calculations, the CPU usage may also be high, which is why a data script task needs to be run on a large-scale cluster.
5. Will threads in the BLOCKED state cause the CPU usage to soar?
answer : Not necessarily
analyzes : The spike in CPU usage is more due to context switching or too many runnable state threads. A thread in a blocked state does not necessarily lead to an increase in CPU usage.
6. What does it mean that the CPU's us
and sy
values are high in the time-sharing operating system?
We can use the top
command to view us
and sy
of the CPU, as shown in the following figure:
- us : The percentage of CPU occupied by user space. Simply put, if
us
is relatively high, it is caused by our program, and it is easy to locate the problematic thread by analyzing the thread stack. - sy : Percentage of CPU occupied by kernel space.
sy
high, if it is caused by the program, it is basically caused by the thread context switch.
Four. Experience
How to locate the cause of high CPU usage? The analysis process is briefly introduced below.
If you find that the CPU usage of an application server is high, first check the thread number, JVM, system load and other parameters, and then use these parameters to prove the cause of the problem. Second, uses jstack to print stack information , and use tools to analyze thread usage (recommended to use online thread analysis tool fastThread).
Online thread analysis tool fastThread Address: https://fastthread.io/
The next article introduces how to use the online thread analysis tool fastThread analyze thread stack information.
Reference documents:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。