Let's say your Java program is taking 100% CPU. It has 50 threads. You need to find which thread is guilty. I have not found a tool that can help. Currently I use the following very time consuming routine:
- Run
jstack <pid>
, where pid is the process id of a Java process. The easy way to find it is to run another utility included in the JDK -jps
. It is better to redirect jstack's output to a file. - Search for "runnable" threads. Skip those that wait on a socket (for some reason they are still marked runnable).
- Repeat steps 1 and 2 a couple of times and see if you can locate a pattern.
Alternatively, you could attach to a Java process in Eclipse and try to suspend threads one by one, until you hit the one that hogs CPU. On a one-CPU machine, you might need to first reduce the Java process's priority to be able to move around. Even then, Eclipse often isn't able to attach to a running process due to a timeout.
I would have expected Sun's visualvm
tool to do this.
Does anybody know of a better way?