Using process explorer to find high CPU usage problem in Java

This article will provide you with a tutorial on how you can quickly pinpoint the Java thread contributors to a high CPU problem on the Windows OS

Troubleshooting tools

The following tools will be used below for this tutorial:

–        Windows Process Explorer (to pinpoint high CPU Thread contributors)

–        JVM Thread Dump (for Thread correlation and root cause analysis at code level)

High CPU simulator Java program

Lets simulate high CPU usage environment. Executing this code will hike your CPU usage by almost 30%. It basically goes through high number of iterations and keep on doing string manipulation

package MemoryCpu;
 
public class HighCPUSimulator2 {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
 
		new th().start();
		new th().start();
	}
 
}
 
class th extends Thread{
	private final static int NB_ITERATIONS = 500000000;
 
	// ~1 KB data footprint
	private final static String DATA_PREFIX = "datadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadata";
 
 
	public void run(){
 
 
		System.out.println("HIGH CPU Simulator 1.0");
		try {
 
			for (int i = 0; i < NB_ITERATIONS; i++) {
 
				// Perform some String manipulations to slowdown and expose
				// looping process...
				String data = DATA_PREFIX + i;
 
			}
 
		} catch (Throwable any) {
			System.out.println("Unexpected Exception! " + any.getMessage()
					+ " [" + any + "]");
 
		}
 
		System.out.println("HighCPUSimulator done!");
 
	}
}

Step#1 Download and install process explorer tool

The Process Explorer tool visually shows the CPU usage dynamically. It is good for live analysis.
You can download Process Explorer from the link below:
http://technet.microsoft.com/en-us/sysinternals/bb896653

Step#2 Run the HighCPUSimulator2 program

Once you run the program, start monitoring process explorer. You can sort it by CPU usage and soon you will see a java process at the top.

Step#3 Right click and open properties

Step#4 Find expensive threads

Sort the threads by CPU usage. You can now see all high CPU consuming threads.
Note down the TID (Thread ID) of expensive threads. This ID is basically native thread ID which is platform specific and not the Java thread ID

Step#5 Take thread dump

Use any memory analyzer tool to take the thread dump at the time of high CPU usage. Jvisualvm is a handy tool
which comes bundled with JDK. Thread dump of above code looks like this :

“Thread-1” #11 prio=5 os_prio=0 tid=0x000000001963d800 nid=0x7170 runnable [0x0000000019d9e000]
java.lang.Thread.State: RUNNABLE
at java.lang.Integer.getChars(Integer.java:460)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:648)
at java.lang.StringBuilder.append(StringBuilder.java:208)
at MemoryCpu.th.run(HighCPUSimulator2.java:36)

Locked ownable synchronizers:
– None

“Thread-0” #10 prio=5 os_prio=0 tid=0x000000001963d000 nid=0x8218 runnable [0x0000000019c9f000]
java.lang.Thread.State: RUNNABLE
at java.lang.Integer.getChars(Integer.java:460)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:648)
at java.lang.StringBuilder.append(StringBuilder.java:208)
at MemoryCpu.th.run(HighCPUSimulator2.java:36)

Locked ownable synchronizers:
– None

Step#6 Mapping Thread IDs

Thread ID you got in step 4 is native thread ID and is in decimal format. In thread dump ‘nid’ represents the
native thread ID.Use any online tool to convert Thread ID got in step 4 into hexadecimal value. I used Decimal to hexadecimal converter

29040 is 7170 in hexa
33304 is 8218 in hexa

Search for 7170 and 8218 in the thread dump. And finally you will find your high CPU consuming threads.
Now you know the code which was causing the CPU hike

Mar Java Mit Java 🙂

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *