The CPU of the server in the production environment soars, how to locate the Java code?

Table of contents

Foreword:

Step by step:

1. Use the top command to find the process that occupies too much CPU:

2. Use the ps -mp command to output the running status list of threads under this PID, as shown in the following figure:

3. Use the printf command to convert the TID to a hexadecimal number:

4. Use the jstack command to output the specific running log of the thread:


Foreword:

The key to solving this problem is to locate the location of the Java code.

The following process takes centos as an example:

Note: It doesn’t matter if you can’t remember the specific operation commands during the interview, as long as you know the cause of the CPU soaring and tell you the solution, the problem is not big.

Step by step:

1. Use the top command to find the process that occupies too much CPU:

After using the top command, you can see a list, which contains information such as PID (process ID), USER (operating user), CPU usage, memory usage, TIME+ (running time), COMMAND (running command), etc., generally according to CPU The occupancy is arranged in descending order from top to bottom, as shown in the figure below:

We found that the command column is a row of Java, indicating that this program is written in Java. Then use Notepad to write down the corresponding PID, which is the process ID.

2. Use the ps -mp command to output the running status list of threads under this PID, as shown in the following figure:

This list contains several key fields, such as CPU usage, TID (thread ID), TIME (running time), etc. Find the thread with the highest CPU usage in this list, and write down the TID, which is the thread ID.

The TID noted above is a decimal number, which cannot be used directly and needs to be converted into a hexadecimal number.

3. Use the printf command to convert the TID to a hexadecimal number:

In this way, the thread ID that really takes up too much CPU is obtained.

4. Use the jstack command to output the specific running log of the thread:

jstack has 3 parameters, the first parameter is the PID recorded earlier, followed by grep, followed by the TID converted into a hexadecimal number, and finally added –A and a number, this number indicates the output log The number of lines, so far you can directly print out the specific exception information.

If there are a lot of log information and the abnormal content is complicated, you can output the abnormal information to a txt file and analyze it slowly. Just add the txt file name at the end of the jstack command.


jstack PID | grep TID -A60 >> error_log.txt

Guess you like

Origin blog.csdn.net/weixin_42717648/article/details/131630766