快速找到Tomcat中最耗CPU的线程

1、找出TOMCAT的JVM的进程ID

[work@112542000 ~]$ jps                                                                                                                                         
290 Bootstrap                                                                                                                                                   
61213 Jps                                                                                                                                                       

2、查看该进程中,最耗费CPU的线程

[work@112542000 ~]$ ps -mp 290 -o THREAD,tid,time | sort -k2 -r | head -n 20                                                                                    
USER     %CPU PRI SCNT WCHAN  USER SYSTEM   TID     TIME                                                                                                        
work     15.3   -    - -         -      -     - 03:11:58                                                                                                        
work      1.2  19    - -         -      -   326 00:15:45                                                                                                        
work      0.2  19    - -         -      -   872 00:03:44                                                                                                        
work      0.1  19    - -         -      -   992 00:01:44                                                                                                        
work      0.1  19    - -         -      -   972 00:01:16                                                                                                        
work      0.1  19    - -         -      -   870 00:01:19                                                                                                        
work      0.1  19    - -         -      -   869 00:01:34                                                                                                        
work      0.0  19    - -         -      -  9993 00:00:00                                                                                                        
work      0.0  19    - -         -      -   997 00:00:06                                                                                                        
work      0.0  19    - -         -      -  9969 00:00:00                                                                                                        
work      0.0  19    - -         -      -  9968 00:00:00                                                                                                        
work      0.0  19    - -         -      -   996 00:00:34                                                                                                        
work      0.0  19    - -         -      -  9960 00:00:00                                                                                                        
work      0.0  19    - -         -      -   995 00:00:00                                                                                                        
work      0.0  19    - -         -      -  9944 00:00:00                                                                                                        
work      0.0  19    - -         -      -   994 00:00:31                                                                                                        
work      0.0  19    - -         -      -  9936 00:00:00                                                                                                        
work      0.0  19    - -         -      -  9934 00:00:26                                                                                                        
work      0.0  19    - -         -      -  9933 00:00:21                                                                                                        

3、以TID==326为例,查看该线程的堆栈

首先,将十进制的326转换成十六进制,可以在线转换:

http://tool.oschina.net/hexconvert

结果等于146

使用jstack查询该线程堆栈:

[work@112542000 ~]$ jstack 290 | grep "0x160" -A 10                                                                                                             
"Timer-2955" daemon prio=10 tid=0x00007f39d3ecd000 nid=0x1602 in Object.wait() [0x00007f38fb273000]                                                             
   java.lang.Thread.State: TIMED_WAITING (on object monitor)                                                                                                    
        at java.lang.Object.wait(Native Method)                                                                                                                 
        at java.util.TimerThread.mainLoop(Timer.java:552)                                                                                                       
        - locked <0x00000007c2a0fc40> (a java.util.TaskQueue)                                                                                                   
        at java.util.TimerThread.run(Timer.java:505)                                                                                                            
                                                                                                                                                                
"Timer-2954" daemon prio=10 tid=0x00007f392481e000 nid=0x15e4 in Object.wait() [0x00007f38fbe7f000]                                                             
   java.lang.Thread.State: TIMED_WAITING (on object monitor)                                                                                                    
        at java.lang.Object.wait(Native Method)                                                                                                                 
        at java.util.TimerThread.mainLoop(Timer.java:552)                                                                                                       
--                                                                                                                                                              
"Timer-2132" daemon prio=10 tid=0x00007f39d367d000 nid=0x1603 in Object.wait() [0x00007f3933f46000]                                                             
   java.lang.Thread.State: TIMED_WAITING (on object monitor)                                                                                                    
        at java.lang.Object.wait(Native Method)                                                                                                                 
        at java.util.TimerThread.mainLoop(Timer.java:552)                                                                                                       
        - locked <0x00000007b44ef3c8> (a java.util.TaskQueue)                                                                                                   
        at java.util.TimerThread.run(Timer.java:505)                                                                                                            
                                                                                                                                                                
"Timer-2131" daemon prio=10 tid=0x00007f39d3b61800 nid=0x15e6 in Object.wait() [0x00007f3934d54000]                                                             
   java.lang.Thread.State: TIMED_WAITING (on object monitor)                                                                                                    
        at java.lang.Object.wait(Native Method)                                                                                                                 
        at java.util.TimerThread.mainLoop(Timer.java:552)                                                                                                       
--                                                                                                                                                              
"Timer-450" daemon prio=10 tid=0x00007f39d0728800 nid=0x160 in Object.wait() [0x00007f39b1312000]                                                               
   java.lang.Thread.State: TIMED_WAITING (on object monitor)                                                                                                    
        at java.lang.Object.wait(Native Method)                                                                                                                 
        at java.util.TimerThread.mainLoop(Timer.java:552)                                                                                                       
        - locked <0x00000007bddf8788> (a java.util.TaskQueue)                                                                                                   
        at java.util.TimerThread.run(Timer.java:505)                                                                                                            
                                                                                                                                                                
"Timer-449" daemon prio=10 tid=0x00007f39ec01f000 nid=0xffc7 in Object.wait() [0x00007f39af5f5000]                                                              
   java.lang.Thread.State: TIMED_WAITING (on object monitor)                                                                                                    
        at java.lang.Object.wait(Native Method)                                                                                                                 
        at java.util.TimerThread.mainLoop(Timer.java:552)                                                                                                       

 

参考资料:

http://scau-fly.iteye.com/blog/1884606

http://www.blogjava.net/hankchen/archive/2012/05/09/377735.html

 

本文地址:http://crazyant.net/2145.html

Tomcat内存分析相关方法(jmap和mat)

Linux环境命令行

首先,根据进程命令,获取运行的tomcat的进程ID

ps aux | grep tomcat | grep java | grep bsc

在第二列可以看到进程ID

然后使用jmap可以查看内存占比:

/home/work/bsc/java/jdk-1.7-7u60/bin/jmap -heap 7840

输出的结果包括了内存各个部分的占比:

[work@xxxx.com logs]$ /home/work/bsc/java/jdk-1.7-7u60/bin/jmap -heap 10433
Attaching to process ID 10433, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.60-b09

using parallel threads in the new generation.
using thread-local object allocation.
Concurrent Mark-Sweep GC

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 8388608000 (8000.0MB)
   NewSize          = 1048576000 (1000.0MB)
   MaxNewSize       = 1048576000 (1000.0MB)
   OldSize          = 5439488 (5.1875MB)
   NewRatio         = 2
   SurvivorRatio    = 8
   PermSize         = 134217728 (128.0MB)
   MaxPermSize      = 268435456 (256.0MB)
   G1HeapRegionSize = 0 (0.0MB)

Heap Usage:
New Generation (Eden + 1 Survivor Space):
   capacity = 943718400 (900.0MB)
   used     = 393328672 (375.1074523925781MB)
   free     = 550389728 (524.8925476074219MB)
   41.67860582139757% used
Eden Space:
   capacity = 838860800 (800.0MB)
   used     = 288471072 (275.1074523925781MB)
   free     = 550389728 (524.8925476074219MB)
   34.388431549072266% used
From Space:
   capacity = 104857600 (100.0MB)
   used     = 104857600 (100.0MB)
   free     = 0 (0.0MB)
   100.0% used
To Space:
   capacity = 104857600 (100.0MB)
   used     = 0 (0.0MB)
   free     = 104857600 (100.0MB)
   0.0% used
concurrent mark-sweep generation:
   capacity = 1288736768 (1229.03515625MB)
   used     = 706435104 (673.7090148925781MB)
   free     = 582301664 (555.3261413574219MB)
   54.816089797478334% used
Perm Generation:
   capacity = 134217728 (128.0MB)
   used     = 65034240 (62.021484375MB)
   free     = 69183488 (65.978515625MB)
   48.45428466796875% used

27139 interned Strings occupying 3055832 bytes.

如上可以清楚的看到内存配置信息、年轻代(包括eden、from、to)、老年代、永久代各自的内存信息和占比。

使用Eclipse的memory analysis tool工具进行详细分析

首先,使用命令将内存dump到文件:

/home/work/bsc/java/jdk-1.7-7u60/bin/jmap -dump:format=b,file=/home/work/tmp/bsc.bin 10433

MAT(Memory Analyzer Tool)工具是eclipse的一个插件,使用起来非常方便,尤其是在分析大内存的dump文件时,可以非常直观的看到各个对象在堆空间中所占用的内存大小、类实例数量、对象引用关系、利用OQL对象查询,以及可以很方便的找出对象GC Roots的相关信息,当然最吸引人的还是能够快速为开发人员生成内存泄露报表,方便定位问题和分析问题。

MAT工具的下载地址为: http://www.eclipse.org/mat/downloads.php

MAT插件的下载地址为: http://download.eclipse.org/mat/1.3/update-site/

mat_image

 

然后就可以查看内存中的数据,点击饼图,也可以看下占比最大的对象是什么,从而分析程序的行为。

本文地址:http://crazyant.net/1980.html