java application cpu too high troubleshooting

I. Introduction

Two days ago, I wrote a subscription message queue program and passed it to Flink, added each subscribed data to the Queue queue, used while(true) to fetch data from the message queue, and the program ran normally, but later found that the CPU usage was too high, which was caused by while(true) during investigation, and now simulates the investigation process of excessive cpu.

2. Test code Test.java

public class Test {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("测试死循环对 CPU 影响");
        for (int i = 0; i < Integer.parseInt(args[0]); i++) {
    
    
            new Thread(()->{
    
    
                System.out.println(Thread.currentThread().getName());
                while (true){
    
    

                }
            },"线程:"+i).start();
        }
    }
}

3. Compile and run the Test.java program on Linux

javac Test.java

Run the Test program and start an infinite loop thread

insert image description here

Fourth, the top command to view the cpu usage

By uppercase C can be sorted by CPU from large to small
insert image description here

It can be seen that the CPU utilization rate of Test is 100%, which is very different from that of window (window CPU 100% is stuck), my Linux server has 2 cores, and the total CPU utilization rate is 50%, and the server will not be stuck. The simple understanding is that one core runs full

5. View the thread details under the processtop -H -p 11748

As shown below, you can see that the CPU usage of 12240 进程under 12227 线程is up to 99.9%.
insert image description here

6. 线程Convert the pid of 12240 to hexadecimal printf "0x%x\n" 12240

The purpose of this step is to retrieve threads from the process snapshot
insert image description here

7. 进程Snapshot viewed by jstack

Use the jstack that comes with java jdk to view 进程the snapshot jstack 12227|grep -A 20 0x2fd0
. You can see that the 7th line of code is caused. From the source code, you can see that it is caused by while(true). Let’s briefly talk about the grep parameter

  • grep -A n displays the n lines that match the specified content and after

  • grep -B n displays the n lines that match the specified content and before

  • grep -C n displays the matching specified content and n lines before and after it

insert image description here
output the entire 进程snapshot to a filejstack 12227 >> jstack.log
insert image description here

Legacy

When I set the number of threads to 2, I found that the 2-core CPU is directly 100%, but the cloud server is not stuck. For
insert image description here
insert image description here
Linux, when I set the number of threads to a number greater than 2, the CPU is close to 200%, which should only affect the execution efficiency and not cause the system to freeze (guess, I have forgotten all the knowledge of the operating system, and I will record it later when I understand it). The main is the case where the number of threads is 5
insert image description here

Guess you like

Origin blog.csdn.net/qq_41538097/article/details/131842679