jvisualvm命令

JVisualVM是JDK自带的性能检测工具,路径在%JAVA_HOME%/bin下,可以识别机器上所有Java进程,可以对堆内存进行dump、快照以及性能可视化分析,也可以安装插件来分析GC趋势、内存消耗详细状况(Visual GC插件)等。

jemalloc 怎么使用 jvm oom dump_java

Jmap命令

Jmap是JDK自带的一种内存映像工具,可以用来查看内存信息,实例个数以及导出dump文件等。

命令帮助

geniusdew@localhost ~ % jmap -help
Usage:
    jmap [option] <pid>
        (to connect to running process)
    jmap [option] <executable <core>
        (to connect to a core file)
    jmap [option] [server_id@]<remote server IP or hostname>
        (to connect to remote debug server)

where <option> is one of:
    <none>               to print same info as Solaris pmap
    -heap                to print java heap summary
    -histo[:live]        to print histogram of java object heap; if the "live"
                         suboption is specified, only count live objects
    -clstats             to print class loader statistics
    -finalizerinfo       to print information on objects awaiting finalization
    -dump:<dump-options> to dump java heap in hprof binary format
                         dump-options:
                           live         dump only live objects; if not specified,
                                        all objects in the heap are dumped.
                           format=b     binary format
                           file=<file>  dump heap to <file>
                         Example: jmap -dump:live,format=b,file=heap.bin <pid>
    -F                   force. Use with -dump:<dump-options> <pid> or -histo
                         to force a heap dump or histogram when <pid> does not
                         respond. The "live" suboption is not supported
                         in this mode.
    -h | -help           to print this help message
    -J<flag>             to pass <flag> directly to the runtime system

常用选项

jmap -histo 查看占用内存大小、实例个数以及类名等信息。

jmap -heap 查看堆各个区的大小、使用情况、以及剩余情况等堆信息。

jmap -dump:format=b,file=文件名 导出堆内存dump文件,可以用jvisualvm命令工具导入dump文件分析。

也可以使用JVM参数设置内存溢出自动导出dump文件:-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=路径

Jstack命令

Jstack是JDK自带的线程堆栈分析工具,可以查看线程堆栈信息。

命令帮助

geniusdew@localhost ~ % jstack -help
Usage:
    jstack [-l] <pid>
        (to connect to running process)
    jstack -F [-m] [-l] <pid>
        (to connect to a hung process)
    jstack [-m] [-l] <executable> <core>
        (to connect to a core file)
    jstack [-m] [-l] [server_id@]<remote server IP or hostname>
        (to connect to a remote debug server)

Options:
    -F  to force a thread dump. Use when jstack <pid> does not respond (process is hung)
    -m  to print both java and native frames (mixed mode)
    -l  long listing. Prints additional information about locks
    -h or -help to print this help message

常用场景

jstack 查找死锁。用jvisualvm也可以自动检测死锁。

jstack找出占用cpu最高的线程堆栈信息。

  1. 使用top -p ,显示java进程的内存情况
  2. 按H,获取每个线程的内存情况
  3. 找到内存和cpu占用最高的线程id,并转为十六进制
  4. 执行 jstack | grep -A 10 <16进制线程id>,得到线程堆栈信息中该线程所在行的后面10行,从堆栈中可以发现导致cpu飙高的调用方法
  5. 查看对应的堆栈信息找出可能存在问题的代码

Jinfo命令

Jinfo是JDK自带的Java配置信息工具,可以实时地查看和调整虚拟机的各项参数。

命令帮助

geniusdew@localhost ~ % jinfo -help
Usage:
    jinfo [option] <pid>
        (to connect to running process)
    jinfo [option] <executable <core>
        (to connect to a core file)
    jinfo [option] [server_id@]<remote server IP or hostname>
        (to connect to remote debug server)

where <option> is one of:
    -flag <name>         to print the value of the named VM flag
    -flag [+|-]<name>    to enable or disable the named VM flag
    -flag <name>=<value> to set the named VM flag to the given value
    -flags               to print VM flags
    -sysprops            to print Java system properties
    <no option>          to print both of the above
    -h | -help           to print this help message

常用选项

jinfo -flags 查看JVM启动参数,可验证启动参数是否生效

jinfo -sysprops 查看Java系统属性

Jstat命令

Jstat是JDK自带的,位于java的bin目录下,主要利用JVM内建的指令对Java应用程序的资源和性能进行实时的命令行的监控,包括了对Heap size和垃圾回收状况的监控,可以用来监视VM内存内的各种堆和非堆的大小及其内存使用量。

常用选项

垃圾回收统计

jstat -gc 评估程序内存使用及GC压力整体情况,最常用命令

jstat -gc 1000 10 每1000ms执行一次,共执行10次

geniusdew@localhost ~ % jps
58033 jar
58051 Jps
geniusdew@localhost ~ % jstat -gc 58033        
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT   
65024.0 69632.0 49232.2  0.0   1122816.0 779435.9  436224.0   114939.1  125312.0 119893.7 15488.0 14498.8     26    0.315   4      0.695    1.010
  • S0C:第一个幸存区的大小,单位KB
  • S1C:第二个幸存区的大小
  • S0U:第一个幸存区的使用大小S1U:第二个幸存区的使用大小
  • EC:伊甸园区的大小
  • EU:伊甸园区的使用大小
  • OC:老年代大小
  • OU:老年代使用大小
  • MC:方法区大小(元空间)
  • MU:方法区使用大小
  • CCSC:压缩类空间大小
  • CCSU:压缩类空间使用大小
  • YGC:年轻代垃圾回收次数
  • YGCT:年轻代垃圾回收消耗时间,单位s
  • FGC:老年代垃圾回收次数
  • FGCT:老年代垃圾回收消耗时间,单位s
  • GCT:垃圾回收消耗总时间,单位s

堆内存统计

jstat -gccapacity

geniusdew@localhost ~ % jstat -gccapacity 58033
 NGCMN    NGCMX     NGC     S0C   S1C       EC      OGCMN      OGCMX       OGC         OC       MCMN     MCMX      MC     CCSMN    CCSMX     CCSC    YGC    FGC 
 87040.0 1397760.0 1277440.0 65024.0 69632.0 1122816.0   175104.0  2796544.0   436224.0   436224.0      0.0 1159168.0 125312.0      0.0 1048576.0  15488.0     26     4

新生代垃圾回收统计

jstat -gcnew

geniusdew@localhost ~ % jstat -gcnew 58033     
 S0C    S1C    S0U    S1U   TT MTT  DSS      EC       EU     YGC     YGCT  
65024.0 69632.0 49232.2    0.0  5  15 69632.0 1122816.0 798298.9     26    0.315

新生代内存统计

jstat -gcnewcapacity

geniusdew@localhost ~ % jstat -gcnewcapacity 58033
  NGCMN      NGCMX       NGC      S0CMX     S0C     S1CMX     S1C       ECMX        EC      YGC   FGC 
   87040.0  1397760.0  1277440.0 465920.0  65024.0 465920.0  69632.0  1396736.0  1122816.0    26     4

老年代垃圾回收统计

jstat -gcold

geniusdew@localhost ~ % jstat -gcold 58033        
   MC       MU      CCSC     CCSU       OC          OU       YGC    FGC    FGCT     GCT   
125312.0 119893.7  15488.0  14498.8    436224.0    114939.1     26     4    0.695    1.010

老年代内存统计

jstat -gcoldcapacity

geniusdew@localhost ~ % jstat -gcoldcapacity 58033
   OGCMN       OGCMX        OGC         OC       YGC   FGC    FGCT     GCT   
   175104.0   2796544.0    436224.0    436224.0    26     4    0.695    1.010

元数据空间统计

jstat -gcmetacapacity

geniusdew@localhost ~ % jstat -gcmetacapacity 58033
   MCMN       MCMX        MC       CCSMN      CCSMX       CCSC     YGC   FGC    FGCT     GCT   
       0.0  1159168.0   125312.0        0.0  1048576.0    15488.0    26     4    0.695    1.010

Arthas

Arthas 是 Alibaba 在 2018 年 9 月开源的 Java 诊断工具。支持 JDK6+, 采用命令行交互模式,可以方便的定位和诊断线上程序运行问题。

官方文档:https://alibaba.github.io/arthas

下载

wget https://alibaba.github.io/arthas/arthas‐boot.jar github下载

运行

java -jar arthas‐boot.jar 可识别机器上所有Java进程

使用:具体命令可查看help或查看官方文档

GCeasy

GCeasy是一款在线的GC日志可视化分析工具,可以上传gc文件,然后利用可视化的界面来展现GC情况,通过GC日志分析进行内存泄露检测、GC暂停原因分析、JVM配置建议优化等功能,部分功能收费。