Find the most CPU-intensive Java threads in your current environment and locate stack information

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.