首页

通过jps、jstack、jmap、jstat指令监控分析当前虚拟机堆栈运行状态情况以实时排除调式运行阶段问题

标签:JMAP,堆栈监控,JSTAT,jstack,jps,mat,内存实时监控     发布时间:2019-07-11   

一、jps、jstack命令

jps命令是 查看但其jvm进程、jstatck命令查看进程的线程运行状态情况

[root@iZuf6gyurl0fqb87hirq23Z tomcat7]# jps@b@22919 Jps@b@19871 Bootstrap@b@@b@[root@iZuf6gyurl0fqb87hirq23Z tomcat7]# jstack  -l 19871@b@2019-07-15 23:00:32@b@Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.80-b11 mixed mode):@b@...

线程状态说明>

- RUNNABLE 线程运行中或 I/O 等待(可能里面还能看到locked字样,表明它获得了某把锁) @b@- BLOCKED 线程受阻塞并等待( synchronized 关键字) @b@- TIMED_WAITING 线程在等待唤醒,但设置了时限 (和WAITING的区别是wait() 等语句加上了时间限制 wait(timeout))@b@- WAITING 线程在无限等待唤醒(等待某个condition或monitor发生,一般停留在park(), wait(), sleep(),join() 等语句里)@b@- TERMINATED,已退出的。

二、jstat命令

jstat命令实时监控classloader,compiler,gc情况

1. 监控classloader加载情况(每隔1秒监控一次,一共做10次)

[root@iZuf6gyurl0fqb87hirq23Z ~]#  jstat -class  19871  1000 10@b@Loaded  Bytes  Unloaded  Bytes     Time   @b@  6648 13167.0        9    14.1       8.11@b@  6648 13167.0        9    14.1       8.11@b@  6648 13167.0        9    14.1       8.11@b@  6648 13167.0        9    14.1       8.11@b@  6648 13167.0        9    14.1       8.11@b@  6648 13167.0        9    14.1       8.11@b@  6648 13167.0        9    14.1       8.11@b@  6648 13167.0        9    14.1       8.11@b@  6648 13167.0        9    14.1       8.11@b@  6648 13167.0        9    14.1       8.11

2.监控gc情况(每隔1秒监控一次,一共做10次)

[root@iZuf6gyurl0fqb87hirq23Z ~]#  jstat -gcutil  19871  1000 10@b@  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT   @b@  4.62   0.00  37.42  77.32  99.33   4106   33.946     4    2.389   36.335@b@  4.62   0.00  37.42  77.32  99.33   4106   33.946     4    2.389   36.335@b@  4.62   0.00  37.42  77.32  99.33   4106   33.946     4    2.389   36.335@b@  4.62   0.00  37.42  77.32  99.33   4106   33.946     4    2.389   36.335@b@  4.62   0.00  37.42  77.32  99.33   4106   33.946     4    2.389   36.335@b@  4.62   0.00  37.42  77.32  99.33   4106   33.946     4    2.389   36.335@b@  0.00   4.25  56.37  77.32  99.33   4107   33.962     4    2.389   36.351@b@  0.00   4.25  56.37  77.32  99.33   4107   33.962     4    2.389   36.351@b@  0.00   4.25  56.42  77.32  99.33   4107   33.962     4    2.389   36.351@b@  0.00   4.25  56.42  77.32  99.33   4107   33.962     4    2.389   36.351

3.VM实时编译的数量等信息(每隔1秒监控一次,一共做10次)

[root@iZuf6gyurl0fqb87hirq23Z ~]# jstat -compiler 19871   1000 10@b@Compiled Failed Invalid   Time   FailedType FailedMethod@b@    2189      2       0    97.58          1 sun/misc/URLClassPath getResource@b@    2189      2       0    97.58          1 sun/misc/URLClassPath getResource@b@    2189      2       0    97.58          1 sun/misc/URLClassPath getResource@b@    2189      2       0    97.58          1 sun/misc/URLClassPath getResource@b@    2189      2       0    97.58          1 sun/misc/URLClassPath getResource@b@    2189      2       0    97.58          1 sun/misc/URLClassPath getResource@b@    2189      2       0    97.58          1 sun/misc/URLClassPath getResource@b@    2189      2       0    97.58          1 sun/misc/URLClassPath getResource@b@    2189      2       0    97.58          1 sun/misc/URLClassPath getResource@b@    2189      2       0    97.58          1 sun/misc/URLClassPath getResource

三、jmap命令

1. 打印堆栈前30条对象class大小详情信息

[root@iZuf6gyurl0fqb87hirq23Z logs]# jmap -histo 15675 | head -30@b@ num     #instances         #bytes  class name@b@----------------------------------------------@b@   1:       1270387       57606600  [C@b@   2:          6211       53647944  [I@b@   3:         40772       48949344  [B@b@   4:       1254021       30096504  java.lang.String@b@   5:        560982       22439280  com.webssky.jcseg.Word@b@   6:        567345       18155040  java.util.concurrent.ConcurrentHashMap$HashEntry@b@   7:        242874       11657952  java.util.HashMap@b@   8:        102979        9889112  [Ljava.util.HashMap$Entry;@b@   9:        289507        9264224  java.util.HashMap$Entry@b@  10:         57704        8531008  <constMethodKlass>@b@  11:         57704        7397408  <methodKlass>@b@  12:          5631        6492744  <constantPoolKlass>@b@  13:        235429        5650296  com.chenlb.mmseg4j.CharNode$TreeNode@b@  14:          5631        3968056  <instanceKlassKlass>@b@  15:        235581        3769296  java.lang.Character@b@  16:          4486        3497568  <constantPoolCacheKlass>@b@  17:          1303        3261328  [Ljava.util.concurrent.ConcurrentHashMap$HashEntry;@b@  18:         19698        1260672  java.net.URL@b@  19:         24673         986920  java.util.LinkedHashMap$Entry@b@  20:         12039         855736  [Ljava.lang.Object;@b@  21:          1265         716904  <methodDataKlass>@b@  22:         10247         640280  [S@b@  23:          6054         585136  java.lang.Class@b@  24:         12001         576048  org.apache.catalina.loader.ResourceEntry@b@  25:         20114         482736  org.apache.tomcat.util.bcel.classfile.ConstantUtf8@b@  26:          8785         450296  [[I@b@  27:          5352         428160  java.lang.reflect.Method@b@  28:         12715         406880  com.chenlb.mmseg4j.CharNode@b@  29:         14391         286376  [Ljava.lang.Class;@b@  30:         10638         255312  java.util.LinkedList$Node
其中:@b@[C is a char[]@b@[S is a short[]@b@[I is a int[]@b@[B is a byte[]@b@[[I is a int[][]

2. 打印堆栈大小占比详情

[root@iZuf6gyurl0fqb87hirq23Z logs]# jmap -heap  15675@b@Attaching to process ID 15675, please wait...@b@Debugger attached successfully.@b@Server compiler detected.@b@JVM version is 24.80-b11@b@@b@using thread-local object allocation.@b@Mark Sweep Compact GC@b@@b@Heap Configuration:@b@   MinHeapFreeRatio = 40@b@   MaxHeapFreeRatio = 70@b@   MaxHeapSize      = 1048576000 (1000.0MB)@b@   NewSize          = 1310720 (1.25MB)@b@   MaxNewSize       = 209715200 (200.0MB)@b@   OldSize          = 5439488 (5.1875MB)@b@   NewRatio         = 2@b@   SurvivorRatio    = 8@b@   PermSize         = 21757952 (20.75MB)@b@   MaxPermSize      = 85983232 (82.0MB)@b@   G1HeapRegionSize = 0 (0.0MB)@b@@b@Heap Usage:@b@New Generation (Eden + 1 Survivor Space):@b@   capacity = 188743680 (180.0MB)@b@   used     = 14213168 (13.554733276367188MB)@b@   free     = 174530512 (166.4452667236328MB)@b@   7.530407375759548% used@b@Eden Space:@b@   capacity = 167772160 (160.0MB)@b@   used     = 14213168 (13.554733276367188MB)@b@   free     = 153558992 (146.4452667236328MB)@b@   8.471708297729492% used@b@From Space:@b@   capacity = 20971520 (20.0MB)@b@   used     = 0 (0.0MB)@b@   free     = 20971520 (20.0MB)@b@   0.0% used@b@To Space:@b@   capacity = 20971520 (20.0MB)@b@   used     = 0 (0.0MB)@b@   free     = 20971520 (20.0MB)@b@   0.0% used@b@tenured generation:@b@   capacity = 838860800 (800.0MB)@b@   used     = 264228688 (251.9881134033203MB)@b@   free     = 574632112 (548.0118865966797MB)@b@   31.49851417541504% used@b@Perm Generation:@b@   capacity = 33226752 (31.6875MB)@b@   used     = 32992568 (31.46416473388672MB)@b@   free     = 234184 (0.22333526611328125MB)@b@   99.29519442646696% used@b@@b@15245 interned Strings occupying 1949432 bytes.
[root@iZuf6gyurl0fqb87hirq23Z dump]# ps  -ef|grep  tomcat@b@root     24175     1 60 23:48 pts/2    00:01:59 /home/app/jdk1.7.0_80/jre/bin/java -Djava.util.logging.config.file=/home/app/tomcat7/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -Xms300M -Xmx790M -XX:HeapDumpPath=/home/app/tomcat7/logs/dump -XX:ParallelGCThreads=4 -XX:PermSize=50M -XX:MaxPermSize=128M -XX:-DisableExplicitGC -XX:+UseCompressedOops -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -Dsolr.solr.home=/home/app/tomcat7/bin/solr-md -Djava.endorsed.dirs=/home/app/tomcat7/endorsed -classpath /home/app/tomcat7/bin/bootstrap.jar:/home/app/tomcat7/bin/tomcat-juli.jar -Dcatalina.base=/home/app/tomcat7 -Dcatalina.home=/home/app/tomcat7 -Djava.io.tmpdir=/home/app/tomcat7/temp org.apache.catalina.startup.Bootstrap start@b@@b@[root@iZuf6gyurl0fqb87hirq23Z dump]#  jmap -dump:file=/home/app/tomcat7/logs/dump/javaDump.hprof,format=b 24175@b@@b@[root@iZuf6gyurl0fqb87hirq23Z dump]# ls  -l@b@@b@total 429188@b@-rw------- 1 root root 439482830 Nov  7 23:51 javaDump.hprof

3. 将当前堆栈信息以二进制格式保存为heap.bytes.data文件(建议通过jdk自带Java VisualVM堆栈查看工具进行装入分析,也可以通过mat工具进行查询分析

[root@iZuf6gyurl0fqb87hirq23Z logs]# jmap  -dump:live,format=b,file=heap.bytes.data  19871@b@Dumping heap to /app/tomcat7/logs/heap.bytes.data20190715 ...@b@Heap dump file created@b@...