Skip to content

Instantly share code, notes, and snippets.

@ochinchina
Created July 24, 2015 07:51
Show Gist options
  • Save ochinchina/4e969eb806e7e7623b92 to your computer and use it in GitHub Desktop.
Save ochinchina/4e969eb806e7e7623b92 to your computer and use it in GitHub Desktop.
use linux top command and the jdk utility jstack to find which java thread consumes most CPU

use top command to find which java thread consume high CPU

Using the following command to find what java threads consume high CPU

$ top -n1 -p <pid> -H
Tasks:  47 total,   0 running,  47 sleeping,   0 stopped,   0 zombie
Cpu(s): 22.1%us,  2.2%sy,  0.0%ni, 74.8%id,  0.0%wa,  0.0%hi,  0.9%si,  0.0%st
Mem:  32955308k total,  4727936k used, 28227372k free,   723000k buffers
Swap: 16875512k total,        0k used, 16875512k free,  3022092k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
22784 nls       24  -1 2430m  45m 9900 S  90.0 0.1   0:00.00 java
22791 nls       18  -1 2430m  45m 9900 S  0.0  0.1   0:00.29 java
22792 nls       17  -1 2430m  45m 9900 S  0.0  0.1   0:00.00 java
22793 nls       17  -1 2430m  45m 9900 S  0.0  0.1   0:00.00 java
22794 nls       17  -1 2430m  45m 9900 S  0.0  0.1   0:00.00 java
22795 nls       17  -1 2430m  45m 9900 S  0.0  0.1   0:00.00 java
22796 nls       17  -1 2430m  45m 9900 S  0.0  0.1   0:00.00 java
22797 nls       17  -1 2430m  45m 9900 S  0.0  0.1   0:00.00 java
22798 nls       17  -1 2430m  45m 9900 S  0.0  0.1   0:00.00 java
22799 nls       17  -1 2430m  45m 9900 S  0.0  0.1   0:00.00 java
22800 nls       17  -1 2430m  45m 9900 S  0.0  0.1   0:00.00 java

From the above top command output, we know the thread 22784 consume high cpu. Convert the number 22784 to hex

$ printf "%x" 22784
5900

so the thread is 5900

dump the java thread stack using jstack

$jstack -l <pid>

find the thread 5900 and the output looks like below:

...

"Thread-11" #62 prio=5 os_prio=0 tid=0x60b45400 nid=0x5900 runnable [0x60776000]
   java.lang.Thread.State: RUNNABLE

...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment