[Development experience] How to solve the problem of high CPU usage in java service production environment


        When the CPU of the Java service production environment suddenly increases and the log query fails, you can locate the problem by using the JVM debugging tool.

ideas

Locate the java service process -> locate the java thread -> locate the code block

1. Locate the java service process

A server may have multiple java services. Use the topcommand to check which service has the highest CPU usage. Remember the PID number with high CPU usage.

image-20211102145534739

2. Locate the thread id

There are many threads executing in each java service, ps H -eo pid,tid,%cpu --sort=%cpu |grep [PID]which thread has higher CPU usage can be located by

[root@]# ps H -eo pid,tid,%cpu --sort=%cpu |grep 20443
20443 22263  0.0
20443 20469  0.3
20443 20451  0.4
20443 20466  0.6
20443 20468  0.6
20443 20467  50.0

As shown in the figure above, you can see that 20467the CPU usage of the thread is high, and you can check which piece of code is using the most.

3. Locate the code block

The jstack [PID]execution state of the thread can be displayed as shown in the figure below, but it is messy, and it is searched by 20467converting to 16进制.

image-20211102151016912

Search for the corresponding thread information, and you can intuitively see the code exception information.

It can be tested through a while loop in springboot.

@GetMapping ("/while")
public void while1(){
    
    
   log.info ("while!~~~~~~~~~");
    while (true){
    
    

    }
}

Guess you like

Origin blog.csdn.net/qq_30285985/article/details/121101267