The introduction

Do not know in everybody interview, have encountered this problem

Several Java programs are deployed on the production server. A cpu100% exception alarm is generated. How can you locate the problem?

This question is answered in two versions! High-profile version sorry, I am doing research and development, this problem is impossible to meet in production! Because it is impossible for r & D to directly operate the production server, if your company can have this problem, you should rethink whether your permission control is reasonable!

What’s going on in the interviewer’s mind?

This is a problem I haven’t encountered in production, because we can’t operate the production environment directly. Let’s just say I’ve seen it before in a test environment. The steps are as follows, Balabala…

The interviewer’s mind activity: the authority control is good, should be in the big factory to stay.

Let’s begin our text

The body of the

The following two systems are given under the investigation steps, are exactly the same, but the command is slightly different!

  • Check the PID of the process that consumes the most CPU

  • Locate the thread number that consumes the most CPU based on the PID

  • Locate the Corresponding Java thread based on the thread id and process the thread.

Prepare a line of code for an infinite loop

public class TestFor { public static void main(String[] args) { int random = 0; while (random < 100) { random = random * 10; }}}Copy the code

How to run, should not need me to say, directly teach everyone how to check!

The Windows version

Some may wonder why I want to say Windows! Because, I have done systems for many government departments. I found out they were using Windows Server, not Linux. All necessary to say!

Check the PID of the process that consumes the most CPU

No Windows Server machine at hand, I take Win 10 as an example, screenshots for you to have a look, first call out PID display items!

Locate the thread number that consumes the most CPU based on the PID

Here use of Microsoft tools Process Explorer v16.22, address https://docs.microsoft.com/zh-cn/sysinternals/downloads/process-explorer as shown below

Locate the Corresponding Java thread based on the thread id and process the thread

Run the following command to export the process snapshot

jstack -l 10856 > c:/10856.stackCopy the code

Open the file C :/10856.stack and search for 19d8, as shown below

TestFor.java

The Linux version

In Linux, the steps are exactly the same, except the commands are changed

Check the PID of the process that consumes the most CPU

Execute the command

  • Run the top -c command to display the process running list. Press P to sort processes by CPU usage

As shown in the figure below, processes with a PID of 3033 consume the most CPU

Locate the thread number that consumes the most CPU based on the PID

Execute the command

  • Top-hp 3033, displays a list of thread running information for a process. Press P to sort processes by CPU usage

As shown in the figure below, threads with a PID of 3034 consume the most CPU

This is decimal data, converted to hexadecimal 0xbda

Locate the Corresponding Java thread based on the thread id and process the thread

Run the following command to export the process snapshot

jstack -l 3033 > ./3033.stackCopy the code

Then run the grep command to see what the thread 0xbda does

cat 3033.stack |grep 'bda' -C 8Copy the code

The output is as follows

The problem has been located

conclusion

Make sure you do it!