Home / Java Patterns and Pitfalls     frequal.com

Debugging Tomcat CPU Hogs

Tomcat Hogging the CPU -- But Which Thread?

Sometimes you have a Tomcat process that is clearly hogging one or more CPU cores. If you see the CPU utilization at 200%, for example, that means 2 cores worth of processing time are being used. What you really want to know is

Finding the thread ID

In Linux, run top -H. This will show the threads of the process broken out individually. Note the process ID (PID) of the offending thread(s).

Next, convert the thread ID(s) to hexadecimal. This will be needed in the next step. On Linux, dc makes quick work of this. Set it to hexadecimal output (16 o) then enter the decimal PID from top and enter p. The hex version of the PID will be displayed.

> dc
16o
1000p
3E8

Identifying the Thread in a Stack Dump

Next, re-run top without -H to find Tomcat's primary PID. Then, run jstack {PID} > stack.txt to record the full stack trace of all threads in Tomcat's JVM. Finally, search through the stack.txt file for the lowercase version of hexadecimal PID you found in the previous step (3e8 in the example above). It should match one thread's 'NID' (native ID). This is the offending thread.
"Thread-1" prio=10 tid=0x00007f441435a000 nid=0x3e8 runnable

Last modified on 18 May 2013 by AO

Copyright © 2024 Andrew Oliver