JVM调优之jstack找出最耗cpu的线程并定位代码分析

在工作当中,肯定会遇到由代码所导致的高CPU耗用以及内存溢出的情况。这种情况发生时,我们怎么去找出原因并解决。
我这里使用top +jstack命令去查找cpu占用比较高的代码。
操作如下:

1.top查看java项目中占用吃cpu比较高的进程。

ps  -ef|grep   weblogic  |grep -v  grep
oracle   14740 14652  9 00:00 ?        00:59:06 /usr/java/jdk1.7.0_55/bin/java -server -Xms2048m -Xmx4096m -XX:MaxPermSize=1024m -Dweblogic.Name=AdminServer ---------

2.使用top -Hp 查看线程占用情况

top -Hp 14740
top - 10:13:59 up 314 days, 21:07,  1 user,  load average: 0.25, 0.38, 0.44
Tasks: 198 total,   0 running, 198 sleeping,   0 stopped,   0 zombie
Cpu(s):  4.0%us,  1.5%sy,  0.0%ni, 94.1%id,  0.0%wa,  0.0%hi,  0.3%si,  0.0%st
Mem:   8174516k total,  8077956k used,    96560k free,   167104k buffers
Swap: 20482864k total,   121696k used, 20361168k free,  3805988k cached
 
   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                                                                                                                        
16627 oracle    15   0 6036m 3.2g  47m S  0.7 41.3   4:52.08 java                                                                                                                                                                                                            
14740 oracle    18   0 6036m 3.2g  47m S  0.0 41.3   0:00.00 java                                                                                                                                                                                                            
14741 oracle    15   0 6036m 3.2g  47m S  0.0 41.3   0:08.38 java                                                                                                                                                                                                            
14742 oracle    15   0 6036m 3.2g  47m S  0.0 41.3   0:42.64 java                                                                                                                                                                                                            
14743 oracle    15   0 6036m 3.2g  47m S  0.0 41.3   0:42.64 java                                                                                                                                                                                                            
14744 oracle    15   0 6036m 3.2g  47m S  0.0 41.3   0:43.50 java                                                                                                                                                                                                            
14745 oracle    15   0 6036m 3.2g  47m S  0.0 41.3   0:00.37 java                                                                                                                                                                                                            
14746 oracle    15   0 6036m 3.2g  47m S  0.0 41.3   0:04.70 java                                                                                                                                                                                                            
14747 oracle    15   0 6036m 3.2g  47m S  0.0 41.3   0:00.00 java                                                                                                                                                                                                            
14748 oracle    15   0 6036m 3.2g  47m S  0.0 41.3   1:24.94 java                                                                                                                                                                                                            
14749 oracle    15   0 6036m 3.2g  47m S  0.0 41.3   1:23.13 java                                                                                                                                                                                                            
14750 oracle    18   0 6036m 3.2g  47m S  0.0 41.3   0:00.00 java                                                                                                                                                                                                            
14751 oracle    15   0 6036m 3.2g  47m S  0.0 41.3   0:00.18 java                                                                                                                                                                                                            
14940 oracle    15   0 6036m 3.2g  47m S  0.0 41.3   0:00.28 java         

3.TIME列就是各个Java线程耗费的CPU时间,CPU时间最长的是线程ID为16627的线程,用
printf "%x\n" 16627
得到21742的十六进制值为40f3。
下面就使用jstack定位这个线程的代码。

jstack  -F  14740   | grep  -A 20 40f3
Attaching to process ID 14741, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.55-b03
java.lang.NullPointerException
     at sun.jvm.hotspot.tools.StackTrace.run(StackTrace.java:78)
     at sun.jvm.hotspot.tools.StackTrace.run(StackTrace.java:45)
     at sun.jvm.hotspot.tools.JStack.run(JStack.java:60)
     at sun.jvm.hotspot.tools.Tool.start(Tool.java:221)
     at sun.jvm.hotspot.tools.JStack.main(JStack.java:86)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     at java.lang.reflect.Method.invoke(Method.java:606)
     at sun.tools.jstack.JStack.runJStackTool(JStack.java:136)
     at sun.tools.jstack.JStack.main(JStack.java:102)

通过上面这段代码可以找出什么原因导致java进程占用那么高CPU资源。

注:有的时候jstack这个命令找不到,可以用locate jstack查找具体路径。如果还没有,那就要确定这个项目是不是java项目了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值