Here is a summary of the steps to find the thread resource consumption problem for Linux, Sun (Oracle) JDK, in Linux, when the Java process is found to occupy a high CPU resource, and want to further find out which Java thread is consuming CPU resources, Follow these steps to find:
- Use the top command to find the Java process ID that consumes resources, such as:
- As shown, the process ID of Java is ' 12377 ', and then all threads in the process are monitored separately with the top command:?
- As shown in Linux, all Java internal threads actually correspond to a process ID, which means that the Sun JVM on Linux maps threads in Java programs to operating system processes, and we see that the process ID that consumes the most CPU resources is ' 15417 ', This process ID corresponds to the ' Nid ' (' n ' stands for ' native ') in the Java thread information;
- To find out exactly which specific code occupies so many resources, first use Jstack to play the current stack of information into a file, such as Stack.log:?
1 |
jstack 12377 > stack.log |
Then use the ' jtgrep ' script to grab the Java thread with the process number ' 15417 ' in Stack.log:
?
Among them, ' Jtgrep ' is a shell script that he writes casually:
?
1234 |
#!/bin/sh nid=`python -c "print hex($1)" ` grep -i $nid $2 |
The truth is simple, is to convert ' 15417 ' into 16, directly grep stack.log, you can see that the grep out of the thread's nid=0x3c39, just 15417 of the 16 binary representation.
Article Source: http://wenyue.me/blog/382
Java: Find the thread that consumes the most CPU resources (how to)