Reference: JVM performance Tuning monitoring Tools JPS, Jstack, Jmap, Jhat, Jstat, hprof use detailed
The following is an example of the most CPU-consuming Java thread in a Java process and locates the stack information, with the command PS, top, printf, Jstack, grep.
The first step is to find out the Java process ID, the Java application name I deployed on the server is Mrf-center
[email protected]:/# ps -ef | grep mrf-center | grep -v greproot 21711 1 1 14:47 pts/3 00:02:10 java -jar mrf-center.jar
Get the process ID of 21711.
The second step is to find the CPU-intensive thread in the process
can use ps-lfp pid or ps-mp pid-o THREAD, Tid, time or top-hp pid, I use the third one here, the output is as follows:
The time column is the CPU for each Java thread, and the longest CPU time is a thread with a thread ID of 21742.
printf "%x\n" 21742
The hexadecimal value of 21742 is 54ee, which is used below.
The third step is to find the Java code for the CPU-intensive thread
It's Jstack's turn, it's used to output the stack information for process 21711, and then grep based on the thread ID's hexadecimal value, as follows:
[email protected]:/# jstack 21711 | grep 54ee"PollIntervalRetrySchedulerThread" prio=10 tid=0x00007f950043e000 nid=0x54ee in Object.wait() [0x00007f94c6eda000]
You can see the CPU consumption in the Pollintervalretryschedulerthread class of object.wait (), I looked up my code, and navigate to the following code:
//Idle wait (). info ( "Thread [" + getname () + ); schedulerthreadstate = Polltaskschedulerthreadstate. idlewaiting ; long now = System. currenttimemillis (); long waitTime = now + getidlewaittime (); long timeuntilcontinue = Waittime-now; synchronized (Siglock) {try {if (!halted. ()) {Siglock. (timeuntilcontinue); }} catch (Interruptedexception ignore) {}}
It is the idle wait code for the polling task, and the above siglock.wait (timeuntilcontinue) corresponds to the previous object.wait ().
Attention:
You can also redirect a thread's stack through the > command to a file by using the Jstack command, and then find the appropriate stack code in the file based on the hexadecimal value of the thread ID.
Find the most CPU-intensive Java threads in your current environment and locate stack information