JDK内置工具使用(jps、jstack、jmap、jstat)

/ jpsjstatjmapjstack / 没有评论 / 26浏览

JDK内置工具使用(jps、jstack、jmap、jstat)

jps

jstack

jstack 就是查看当前 Java程序内线程详细堆栈信息的工具

jmap

jmap 命令可以获得运行中的 jvm的堆的快照,从而可以离线分析堆,以检查内存泄漏,检查一些严重影响性能的大对象的创建,检查系统中什么对象最多,各种对象所占内存的大小等等。

打印heap的概要信息

jmap -heap PID

2020320174850-jmap-heap-summary

查看堆内存中的对象数目、大小

$ jmap -histo:live 19931 | less

2020320175018-heap-statistics

内存dump文件

$ jhat  -port  9998  文件名.dump

该命令通常用来分析内存泄漏OOM,通常做法是:

jstat

Jstat用于查看gc垃圾回收使用情况:

➜  ~ jstat -help
Usage: jstat -help|-options
       jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]

Java7以及之前的Heap模型,java8以及之后已经没有Perm了,被Metaspace (元空间)取代:

|<--Minor GC->|     |<--------Major GC-------->|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
||            |  |  |                          |                       ||
||     Eden   |s0|s1|         Old Memory       |         Perm          ||
||            |  |  |                          |                       ||
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|<--------------JVM Heap(-Xms -Xmx)----------->|   -XX:PermSize
|<-Young Gen(-Xmn)->|                              -XX:MaxPermSize

类加载统计

$ jps -ml | grep clockinout
16163 com.zhangaoo.clockinout.Application
$ jstat -class 16163       
Loaded  Bytes  Unloaded  Bytes     Time   
 11413 21630.8        1     1.6       5.08

垃圾回收统计

jstat -gc 16163 1000 10
S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT   
8704.0 8704.0 4602.0  0.0   69952.0  54161.4   174784.0   29357.1   24320.0 22719.6 2816.0 2513.7    206    0.719   1      0.043    0.762
8704.0 8704.0 4602.0  0.0   69952.0  54161.4   174784.0   29357.1   24320.0 22719.6 2816.0 2513.7    206    0.719   1      0.043    0.762
8704.0 8704.0 4602.0  0.0   69952.0  54161.4   174784.0   29357.1   24320.0 22719.6 2816.0 2513.7    206    0.719   1      0.043    0.762
8704.0 8704.0 4602.0  0.0   69952.0  54161.4   174784.0   29357.1   24320.0 22719.6 2816.0 2513.7    206    0.719   1      0.043    0.762
8704.0 8704.0 4602.0  0.0   69952.0  54161.4   174784.0   29357.1   24320.0 22719.6 2816.0 2513.7    206    0.719   1      0.043    0.762
8704.0 8704.0 4602.0  0.0   69952.0  54161.4   174784.0   29357.1   24320.0 22719.6 2816.0 2513.7    206    0.719   1      0.043    0.762
8704.0 8704.0 4602.0  0.0   69952.0  54161.4   174784.0   29357.1   24320.0 22719.6 2816.0 2513.7    206    0.719   1      0.043    0.762
8704.0 8704.0 4602.0  0.0   69952.0  54180.3   174784.0   29357.1   24320.0 22719.6 2816.0 2513.7    206    0.719   1      0.043    0.762
8704.0 8704.0 4602.0  0.0   69952.0  54180.3   174784.0   29357.1   24320.0 22719.6 2816.0 2513.7    206    0.719   1      0.043    0.762
8704.0 8704.0 4602.0  0.0   69952.0  54180.3   174784.0   29357.1   24320.0 22719.6 2816.0 2513.7    206    0.719   1      0.043    0.762

堆内存统计

$ jstat -gccapacity 17265
 NGCMN    NGCMX     NGC     S0C   S1C       EC      OGCMN      OGCMX       OGC         OC       MCMN     MCMX      MC     CCSMN    CCSMX     CCSC    YGC    FGC 
 87360.0  87360.0  87360.0 8704.0 8704.0  69952.0   174784.0   174784.0   174784.0   174784.0      0.0 1071104.0  24320.0      0.0 1048576.0   2816.0    206     1
# 字段解释
NGCMN: Minimum new generation capacity (kB).
NGCMX: Maximum new generation capacity (kB).
NGC: Current new generation capacity (kB).
S0C: Current survivor space 0 capacity (kB).
S1C: Current survivor space 1 capacity (kB).
EC: Current eden space capacity (kB).
OGCMN: Minimum old generation capacity (kB).
OGCMX: Maximum old generation capacity (kB).
OGC: Current old generation capacity (kB).
OC: Current old space capacity (kB).
MCMN: Minimum metaspace capacity (kB).
MCMX: Maximum metaspace capacity (kB).
MC: Metaspace capacity (kB).
CCSMN: Compressed class space minimum capacity (kB).
CCSMX: Compressed class space maximum capacity (kB).
CCSC: Compressed class space capacity (kB).
YGC: Number of young generation GC events.
FGC: Number of full GC events.

垃圾回收概要统计

jstat -gcutil 17265 1000 10
 S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
 52.87   0.00  80.00  16.80  93.42  89.26    206    0.719     1    0.043    0.762
 52.87   0.00  80.00  16.80  93.42  89.26    206    0.719     1    0.043    0.762
 52.87   0.00  80.00  16.80  93.42  89.26    206    0.719     1    0.043    0.762
 52.87   0.00  80.00  16.80  93.42  89.26    206    0.719     1    0.043    0.762
 52.87   0.00  80.00  16.80  93.42  89.26    206    0.719     1    0.043    0.762
 52.87   0.00  80.00  16.80  93.42  89.26    206    0.719     1    0.043    0.762
 52.87   0.00  80.00  16.80  93.42  89.26    206    0.719     1    0.043    0.762
 52.87   0.00  80.00  16.80  93.42  89.26    206    0.719     1    0.043    0.762
 52.87   0.00  80.00  16.80  93.42  89.26    206    0.719     1    0.043    0.762
 52.87   0.00  80.00  16.80  93.42  89.26    206    0.719     1    0.043    0.762