JDK 自带 JVM 分析工具详解

本文基于 JDK 中自带的 JVM 工具来讲解,内容有

  • JDK 自带哪些 JVM 分析工具
  • 每个工具的具体使用场景


JDK 中自带的工具

目前,针对 Java 应用在运行过程中出现的大部分 JVM 问题(如 OOM,FGC 等),我们都可通过 JDK 中自带的一些工具来分析解决。JDK 自带的工具可在 JDK 的安装目录的 bin 文件夹下找到。见图 1。
在这里插入图片描述

图 1

在图 1 众多的命令中,几个常用命令需特别熟悉

  • jps
  • jinfo
  • jstack
  • jmap
  • jstat
  • jhat

以及不是 jdk 自带的 VisualVM。每个命令具体使用见下文。


使用技巧

每个工具都设置了若干选项,用于查看不同层面的信息,若不清楚每个工具应该配置什么参数,可通过

> cmd -help

来查看命令的使用方式,如查看 jps 使用方式。

> jps -help
usage: jps [-help]
       jps [-q] [-mlvV] [<hostid>]

Definitions:
    <hostid>:      <hostname>[:<port>]

当使用 jps 不带任何可选参数时,将返回 pid(进程号)及进程名称。
在这里插入图片描述

图 2

当仅使用 -q 参数时,将只返回 pid。
在这里插入图片描述

图 3


各工具使用场景

jps

jps 用于查看当前 JVM 上运行的 Java 进程。使用过 Linux 的同学都知道,Linux 系统下有一个 ps 命令,用于列出系统的所有进程,可配合不同的参数选项,查看不同层面的信息。jps 具体的使用方式可见图 2、图 3,此处不累述。

jinfo

jinfo 全称 Java Configuration info 可用于查看或设置(部分可动态配置的)

  • 具体的 pid 信息
  • 可执行文件信息
  • 远程服务信息

其使用方式如下

> 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 查看指定 pid 的信息 即可。
使用方式有以下三种

  • 仅使用 -flag(s) 可选参数
    使用 -flags 查看所有 VM 参数信息

    > jinfo -flags pid	
    

    在这里插入图片描述

    图 4

    或使用 -flag name 查看指定 name 的参数信息。(其实使用 -flags 已经可查看所有的 VM 参数,个人觉得没有特别大的必要使用 -flag 那么 查看单个 VM 参数信息)

    > jinfo -flag name pid
    
  • 仅使用 -sysprops 可选参数
    使用 -sysprops 参数查看系统信息,系统信息较多,这里不展示结果,感兴趣的同学可以自己在命令行输入下述命令查看。

    > jinfo -sysprops pid
    
  • 不使用任何可选参数

    > jinfo pid
    

    不使用任何参数查看进程信息时,将返回 -flags 和 -sysprops 两个参数的全部信息。

从图 4 可知,使用 jinfo 可查看到的 VM 参数信息有

  • 初始堆大小 InitialHeapSize
  • 最大堆大小 MaxHeapSize
  • 最大新生代大小 MaxNewSize
  • 老年代大小 OldSize
  • 垃圾回收算法 +UseParallelGC

jmap

jmap 用于查看指定 pid 进程内存相关的信息。与 jinfo 类似,我们仅关注与具体进程相关的信息,不关注具体文件或远程服务的信息。

> 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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值