Java 性能分析工具介绍

发布时间: 更新时间: 总字数:1448 阅读时间:3m 作者:IP:上海 网址

JDK 提供了很多 JVM 性能调优监控工具,包括 jps、jstack、jmap、jhat、jstat、hprof 等,用户可以检测 Java 应用或者程序片段的状况。

总揽

命令作用
jps基础工具
jstack查看某个 Java 进程内的线程堆栈信息
jmapjmap 导出堆内存,然后使用 jhat 来进行分析
jhatjmap 导出堆内存,然后使用 jhat 来进行分析
jstatJVM 统计监测工具
hprofhprof 能够展现 CPU 使用率,统计堆内存使用情况

导出 HeapDump 文件

  • 内存 OOM 时导出
text
java -jar -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/heap.hprof <server>.jar

jps

jps 主要用来输出 JVM 中运行的进程状态信息,实际中这是最常用的命令,下面要介绍的小工具更多的都是先要使用 jps 查看出当前有哪些 Java 进程,获取该 Java 进程的 id 后再对该进程进行处理。

bash
[root@xiexianbin_cn ~]# jps -help
usage: jps [-help]
       jps [-q] [-mlvV] [<hostid>]

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

说明:

  • -q: 只显示 VM 标示,不显示 jar,class, main 参数等信息。
  • -m: 输出主函数传入的参数。
  • -l: 输出应用程序主类完整 package 名称或 jar 完整名称。
  • -v: 列出 jvm 启动参数。
  • -V: 输出通过.hotsportrc 或-XX:Flags=指定的 jvm 参数。
  • -Joption: 传递参数到 javac 调用的 java lancher。

jstack

jstack 用于生成 java 虚拟机当前时刻的线程快照。jstack 命令主要用来查看 Java 线程的调用堆栈的,可以用来分析线程问题。

线程的几种状态:

  • NEW,未启动的。不会出现在 Dump 中。
  • RUNNABLE,在虚拟机内执行的。
  • BLOCKED,受阻塞并等待监视器锁。
  • WATING,无限期等待另一个线程执行特定操作。
  • TIMED_WATING,有时限的等待另一个线程的特定操作。
  • TERMINATED,已退出的。

使用:

bash
[root@xiexianbin_cn ~]# 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
text
jstack -l pid

jmap

jmap 是 JDK 自带的工具软件,主要用于打印指定 Java 进程(或核心文件、远程调试服务器)的共享对象内存映射或堆内存细节。可以使用 jmap 生成 Heap Dump。

text
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

运行时导出 heap

  • 运行时导出 heap 文件,示例
text
jmap -dump:live,format=b,file=heap.hprof <pid>

jhat

jhat 命令解析会 Java 堆 dump 并启动一个 web 服务器,然后就可以在浏览器中查看堆的 dump 文件了

bash
[root@xiexianbin_cn ~]# jhat -help
Usage:  jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>

    -J<flag>          Pass <flag> directly to the runtime system. For
              example, -J-mx512m to use a maximum heap size of 512MB
    -stack false:     Turn off tracking object allocation call stack.
    -refs false:      Turn off tracking of references to objects
    -port <port>:     Set the port for the HTTP server.  Defaults to 7000
    -exclude <file>:  Specify a file that lists data members that should
              be excluded from the reachableFrom query.
    -baseline <file>: Specify a baseline object dump.  Objects in
              both heap dumps with the same ID and same class will
              be marked as not being "new".
    -debug <int>:     Set debug level.
                0:  No debug output
                1:  Debug hprof file parsing
                2:  Debug hprof file parsing, no server
    -version          Report version number
    -h|-help          Print this help and exit
    <file>            The file to read

For a dump file that contains multiple heap dumps,
you may specify which dump in the file
by appending "#<number>" to the file name, i.e. "foo.hprof#3".

All boolean options default to "true"

jstat

jstat(JVM Statistics Monitoring Tool)是用于监控虚拟机各种运行状态信息的命令行工具。他可以显示本地或远程虚拟机进程中的类装载、内存、垃圾收集、JIT 编译等运行数据,在没有 GUI 图形的服务器上,它是运行期定位虚拟机性能问题的首选工具。

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

Definitions:
  <option>      An option reported by the -options option
  <vmid>        Virtual Machine Identifier. A vmid takes the following form:
                     <lvmid>[@<hostname>[:<port>]]
                Where <lvmid> is the local vm identifier for the target
                Java virtual machine, typically a process id; <hostname> is
                the name of the host running the target Java virtual machine;
                and <port> is the port number for the rmiregistry on the
                target host. See the jvmstat documentation for a more complete
                description of the Virtual Machine Identifier.
  <lines>       Number of samples between header lines.
  <interval>    Sampling interval. The following forms are allowed:
                    <n>["ms"|"s"]
                Where <n> is an integer and the suffix specifies the units as
                milliseconds("ms") or seconds("s"). The default units are "ms".
  <count>       Number of samples to take before terminating.
  -J<flag>      Pass <flag> directly to the runtime system.

jar xvf

  • jinfo

产生 OOM 的原因

  • java.lang.OutOfMemoryError 产生原因是因为内存不够,可以通过增加 Xmx 避免该现象
  • Java 容器 OOM Killed 产生原因是因为 堆外内存 + 堆内存 总体超出限制 limit.request 导致的容器 Kill 行为,该行为不会产生 heapdump 文件