Identify Java code consuming high CPU in Linux (linking JVM thread and Linux PID) February 9, 2011 |
By rameshj@zohocorp.com | in
Applications Manager
We can easily identify a problematic Java code which leads to high CPU utilization in Linux. Let me explain the steps with the following example,
- Package test;
- Public class threaddumptest {
- Public void test (){
- For (INT I = 0; I <10; I ++ ){
- Thread th = new thread (New tr (I ));
- Th. setname ("mythread-" + (1000 + I ));
- Th. Start ();
- }
- }
- Public static void main (string [] ARGs ){
- Threaddumptest T = new threaddumptest ();
- T. Test ();
- }
- Private class tr implements runnable {
- Int ins = 0;
- TR (int I ){
- INS = I;
- }
- Public void run (){
- While (true ){
- If (INS! = 5 ){
- Try {
- Thread. Sleep (10000 );
- } Catch (exception e ){
- E. printstacktrace ();
- }
- }
- }
- }
- }
- }
In the above example, all the threads are in while loop. Wait t 'mythread-000000' thread, all other threads will sleep 10 secs inside the loop. The 'mythread-2017'
Thread will not enter sleep part, so it will run in while loop without sleep. Due to while loop, The 'mythread-00' thread will leads to high CPU utilization.
How to identify?
Step 1:
Execute 'top' command on the console. You can see the 'java' command with PID 7074 utilized
97% of CPU.
Step 2:
The top command displays the process list like the above image. Press 'shift + H' and wait few secs. You can see 'show threads on 'message in the top console.
Now, you can see Thread level details like CPU/memory utilization. You can see a 'java' command thread with PID 7087 utilized
94% of CPU.
Step 3:
The identified problematic thread PID (7087) is in decimal format.
Convert it into hexadecimal format. The respective hexadecimal for 7087 is 1baf. and convert it into lowercase (1baf ).
Step 4:
Take thread dump and search the converted hexadecimal PID (1baf) in the thread dump. you can find the hex PID as 'nid' (with '0x ') of the thread trace. from the trace,
You can find 'mythread-1005 'is the problematic thread in the test program. And using the thread trace you can identify the problematic code and fix the problem.
- [Rameshj @ rameshj thread] $ Java test/threaddumptest
- ^ \ Full thread dump Java hotspot (TM) Client VM (1.5.0 _ 06-b05 mixed mode ):
- "Destroyjavavm" PRIO = 1 tid = 0x083d2d78 nid = 0x1ba2 waiting on condition [0x00000000 .. 0xbff4bf60]
- "MyThread-1009" PRIO = 1 tid = 0x08510a88 nid = 0x1bb3 waiting on condition [0xa9cca000 .. 0xa9ccb020]
- At java. Lang. thread. Sleep (native method)
- At test. threaddumptest $ tr. Run (threaddumptest. Java: 45) & nbs
P;
- At java. Lang. thread. Run (thread. Java: 595)
- "MyThread-1008" PRIO = 1 tid = 0 × 08510318 nid = 0x1bb2 waiting on condition [0xa9d4b000 .. 0xa9d4bea0]
- At java. Lang. thread. Sleep (native method)
- At test. threaddumptest $ tr. Run (threaddumptest. Java: 45)
- At java. Lang. thread. Run (thread. Java: 595)
- "MyThread-1007" PRIO = 1 tid = 0x0850f298 nid = 0x1bb1 waiting on condition [0xa9dcc000 .. 0xa9dccf20]
- At java. Lang. thread. Sleep (native method)
- At test. threaddumptest $ tr. Run (threaddumptest. Java: 45)
- At java. Lang. thread. Run (thread. Java: 595)
- "MyThread-1006" PRIO = 1 tid = 0x0850d818 nid = 0x1bb0 waiting on condition [0xa9e4d000 .. 0xa9e4dda0]
- At java. Lang. thread. Sleep (native method)
- At test. threaddumptest $ tr. Run (threaddumptest. Java: 45)
- At java. Lang. thread. Run (thread. Java: 595)
- "MyThread-1005" PRIO = 1 tid = 0x0850c9f8 nid = 0x1baf runnable [0xa9ece000 .. 0xa9ecee20]
- At test. threaddumptest $ tr. Run (threaddumptest. Java: 43)
- At java. Lang. thread. Run (thread. Java: 595)
- "MyThread-1004" PRIO = 1 tid = 0x0850b8e8 nid = 0x1bae waiting on condition [0xa9f4f000... 0xa9f500a0]
- At java. Lang. thread. Sleep (native method)
- At test. threaddumptest $ tr. Run (threaddumptest. Java: 45)
- At java. Lang. thread. Run (thread. Java: 595)
- "MyThread-1003" PRIO = 1 tid = 0x0850aac8 nid = 0x1bad waiting on condition [0xa9fd0000 .. 0xa9fd1120]
- At java. Lang. thread. Sleep (native method)
- At test. threaddumptest $ tr. Run (threaddumptest. Java: 45)
- At java. Lang. thread. Run (thread. Java: 595)
- "MyThread-1002" PRIO = 1 tid = 0x085099b8 nid = 0x1bac waiting on condition [0xaa051000 .. 0xaa051fa0]
- At java. Lang. thread. Sleep (native method)
- At test. threaddumptest $ tr. Run (threaddumptest. Java: 45)
- At java. Lang. thread. Run (thread. Java: 595)
- "MyThread-1001" PRIO = 1 tid = 0x08508a28 nid = 0x1bab waiting
On condition [0xaa0d2000... 0xaa0d3020]
- At java. Lang. thread. Sleep (native method)
- At test. threaddumptest $ tr. Run (threaddumptest. Java: 45)
- At java. Lang. thread. Run (thread. Java: 595)
- "MyThread-1000" PRIO = 1 tid = 0x08508520 nid = 0x1baa waiting on condition [0xaa153000 .. 0xaa153ea0]
- At java. Lang. thread. Sleep (native method)
- At test. threaddumptest $ tr. Run (threaddumptest. Java: 45)
- At java. Lang. thread. Run (thread. Java: 595)
- "Low memory detector" daemon PRIO = 1 tid = 0x0843cf58 nid = 0x1ba8 runnable [0x00000000 .. 0x00000000]
- "Compilerthread0" daemon PRIO = 1 tid = 0x0843b930 nid = 0x1ba7 waiting on condition [0x00000000 .. 0xaa3eb808]
- "Signal dispatcher" daemon PRIO = 1 tid = 0x0843a9d0 nid = 0x1ba6 waiting on condition [0x00000000 .. 0x00000000]
- "Finalizer" daemon PRIO = 1 tid = 0 × 08430740 nid = 0x1ba5 in object. Wait () [0xaa52c000... 0xaa52d120]
- At java. Lang. Object. Wait (native method)
- -Waiting on <0xaa6b0848> (a java. Lang. Ref. referencequeue $ lock)
- At java. Lang. Ref. referencequeue. Remove (referencequeue. Java: 116)
- -Locked <0xaa6b0848> (a java. Lang. Ref. referencequeue $ lock)
- At java. Lang. Ref. referencequeue. Remove (referencequeue. Java: 132)
- At java. Lang. Ref. finalizer $ finalizerthread. Run (finalizer. Java: 159)
- "Reference handler" daemon PRIO = 1 tid = 0x0842e898 nid = 0x1ba4 in object. Wait () [0xaa5ad000 .. 0xaa5adfa0]
- At java. Lang. Object. Wait (native method)
- -Waiting on <0xaa6b0758> (a java. Lang. Ref. Reference $ lock)
- At java. Lang. Object. Wait (object. Java: 474)
- At java. Lang. Ref. Reference $ referencehandler. Run (reference. Java: 116)
- -Locked <0xaa6b0758> (a java. Lang. Ref. Reference $ lock)
- "VM thread" PRIO = 1 tid = 0x0842bb68 nid = 0x1ba3 runnable
- "VM periodic task thread" PRIO = 1 tid = 0x0843e538 nid = 0x1ba9 waiting on condition
Update: FYI: You can automatically take a thread dump of a Java application when a high CPU threshold is violated by using applications
Manager. Learn more or
Watch a related video or
Download applications manager now.
-Ramesh
From: http://blogs.manageengine.com/appmanager/2011/02/09/identify-java-code-consuming-high-cpu-in-linux-linking-jvm-thread-and-linux-pid/