Identify Java code consuming high CPU in Linux
来源:互联网 发布:linux acpi驱动 编辑:程序博客网 时间:2024/05/31 19:35
Identify Java code consuming high CPU in Linux (linking JVM thread and Linux PID)
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. Except ‘MyThread-1005‘ thread, all other threads will sleep 10 secs inside the loop. The ‘MyThread-1005‘ thread will not enter sleep part, so it will run in while loop without sleep. Due to while loop, the ‘MyThread-1005‘ thread will leads to high CPU utilization.
How to identify ?
Step 1 :
Step 2 :
Step 3:
Step 4:
- [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=0×08508520 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 orwatch a related video ordownload 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/
- Identify Java code consuming high CPU in Linux
- Linux下high CPU分析心得
- Linux下high CPU分析心得
- (个人开源)vim-ide - consuming less finger, less brain and less cpu to read and write code
- How to write the fast code/ high performance in C#
- Feature: High Memory In The Linux Kernel
- Feature: High Memory In The Linux Kernel
- High Memory In The Linux Kernel(Cited)
- Implementing High Performance Parsers in Java
- CPU frequency and voltage scaling code in the Linux(TM) kernel
- Consuming an OData feed in MVC
- Consuming JSON Strings in SQL Server
- Consuming .Net WCF Rest in different ways
- (转)Consuming Web Services in Oracle
- Consuming XML Web Services in iPhone Applications
- 网站High CPU分析
- Manage CPU cores in Linux
- Check Error Code in Linux
- Eclipse快捷键大全
- to be done【STL】
- hadoop学习笔记(2) 伪分布模式配置
- STM32 关于复位类型的判断及软件复位 bug
- C++垫片类(2)
- Identify Java code consuming high CPU in Linux
- 使用Try…Catch块进行T-SQL错误处理
- list_head详解
- Linux下的Memcache安装
- Android中带有进度条百分比显示的可以从网络下载文件的适配器【单线程模式】
- VC++中进程间相互通信的十一种方法
- JQuery对checkbox操作 (循环获取)
- これ/それ/あれ/どれ
- Myeclipse编辑区背景配色