JVM-10虚拟机性能监控与故障处理工具之【JDK的命令行】

简介: JVM-10虚拟机性能监控与故障处理工具之【JDK的命令行】

思维导图


20180806180440783.png

概述


前面介绍了虚拟机的内存分配和回收,大致有了一些理论基础,接下来我们从实践的角度去了解虚拟机内存管理。定位问题的时候,知识、经验是关键基础,数据是依据,工具是运用知识处理数据的手段。

这里我们说的数据主要包括

  • 运行日志
  • 异常堆栈
  • GC日志
  • 线程快照(thread dump / javacore文件)
  • 堆转储快照(heapdump/hprof文件)等


命令行工具


Java安装目录 bin目录下有很多官方提供的命令行程序,今天我们俩了解下虚拟机性能监控与故障处理工具


image.png


jps 虚拟机进程状况工具

概述

Java Virtual Machine Process Status Tool


20180806193234703.png



不仅名字像unix主机的ps命令,功能也相似,可以列举出正在运行的虚拟机进程,并显示虚拟机执行主类(Main Class,main函数所在的类)名称以及这些进程的本地虚拟机唯一ID(Local Virtual Machine Identifier ,LVMID)。.


使用频率最高的JDK命令行工具,以为内其他的JDK工具大多需要输入它查询到LVMID来确定要监控的是哪一个虚拟机进程。 对于本地虚拟机进程来讲,LVMID和操作系统的进程ID(Process Identifer ,PID) 一致。


语法及使用

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


jps可以通过RMI协议查看开启了RMI服务的远程虚拟机进程状态。,hostid为RMI注册表中注册的主机名。

jps的其他常用选项


选项 作用
-q 只输出LVMID,省略主类的名称
-m 输出虚拟机进程启动时传递给主类main函数的参数
-l 输出主类的全名,如果执行的是jar包,输出jar路径
-v 输出虚拟机进程启动时jvm参数


jstat 虚拟机统计信息监视工具

概述


2018080620380683.png


jstat (JVM Statistics Monitoring Tool)用于监视虚拟机各种运行状态信息的命令行工具。 它可以显示本地或者远程虚拟机进程中的类装载、内存、垃圾会搜、JIT编译等运行数据。


在没有图形化界面的服务器上,它将是运行期定位虚拟机性能问题的首选工具


语法及使用

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.



  • 对于命令格式中的VMID和LVMID,如果是本地虚拟机进程,两者一致。如果是远程虚拟机进程,则VMID的格式为
    [protocol:][//]lvmind[@hostname[:port]/servername]
  • interval : 查询间隔
  • count:查询次数 如果省略interval和count,则只查询一次

假设需要250毫秒查询一次进程3245的垃圾收集状况,一共查询20次

jstat -gc 3245 250 20


  • 选项option代表用户希望查询的虚拟机信息,主要分为3类:类装载、垃圾收集、运行期编译状况。

jstat的主要选项

image.png


使用举例

E:\Program Files\Java\jdk1.8.0_161\bin>jstat -gcutil 12888  1000  10
Warning: Unresolved Symbol: sun.gc.metaspace.capacity substituted NaN
Warning: Unresolved Symbol: sun.gc.metaspace.used substituted NaN
Warning: Unresolved Symbol: sun.gc.metaspace.capacity substituted NaN
Warning: Unresolved Symbol: sun.gc.compressedclassspace.capacity substituted NaN
Warning: Unresolved Symbol: sun.gc.compressedclassspace.used substituted NaN
Warning: Unresolved Symbol: sun.gc.compressedclassspace.capacity substituted NaN
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT
  0.00  91.47  46.39   9.12      -      -      9    0.215     0    0.000    0.215
  0.00  91.47  48.39   9.12      -      -      9    0.215     0    0.000    0.215
  0.00  91.47  48.39   9.12      -      -      9    0.215     0    0.000    0.215
  0.00  91.47  48.39   9.12      -      -      9    0.215     0    0.000    0.215
  0.00  91.47  48.39   9.12      -      -      9    0.215     0    0.000    0.215
  0.00  91.47  48.39   9.12      -      -      9    0.215     0    0.000    0.215
  0.00  91.47  48.39   9.12      -      -      9    0.215     0    0.000    0.215
  0.00  91.47  48.39   9.12      -      -      9    0.215     0    0.000    0.215
  0.00  91.47  48.39   9.12      -      -      9    0.215     0    0.000    0.215
  0.00  91.47  48.39   9.12      -      -      9    0.215     0    0.000    0.215


  • S0: Survivor0是空的
  • S1:Survivor1使用了91.47的空间
  • E:Eden 使用了 48.39%的空间
  • O: 老年代使用了 9.12%的空间
  • M: 因为我这里是使用了JDK8 ,没有了永久代的概念,这里是元数据区
  • YGC: Minor GC表示程序启动以来,发生了9次Minor GC
  • YGCT:Young GC Time 耗时为0.215秒
  • FGC:Full GC 表示程序启动以来,发生了0次Minor GC
  • FGCT:Full GC Time 耗时为0秒
  • GCT:表示GC Time ,所有的GC总耗时 0.215秒


纯文本状态下监视虚拟机的变化确实不如后面提到的VisualVM等可视化的建施工局直接用图形的方式展现直观,但直接在主机上使用jstat命令仍然是一种常用的监控方式。


jinfo Java配置信息工具

概述


20180806203736426.png


语法及使用

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

使用举例

E:\Program Files\Java\jdk1.8.0_161\bin>jinfo -flag MaxPermSize 12888
-XX:MaxPermSize=85983232


jmap Java 内存映射工具

概述


image.png


语法及使用

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的主要选项


image.png


使用举例, 对pid=12888的进程输出dump信息

E:\Program Files\Java\jdk1.8.0_161\bin>jmap -dump:format=b,file=test.bin 12888
Dumping heap to E:\Program Files\Java\jdk1.8.0_161\bin\test.bin ...
Heap dump file created


jhat 虚拟机堆转储快照分析工具

概述


20180806205108530.png

语法及使用

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"


用法举例

E:\Program Files\Java\jdk1.8.0_161\bin>jmap -dump:format=b,file=test.bin 12888
Dumping heap to E:\Program Files\Java\jdk1.8.0_161\bin\test.bin ...
File exists
E:\Program Files\Java\jdk1.8.0_161\bin>jhat test.bin
Reading from test.bin...
Dump file created Mon Aug 06 20:49:25 CST 2018
Snapshot read, resolving...
Resolving 244224 objects...
Chasing references, expect 48 dots................................................
Eliminating duplicate references................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.


浏览器输入 http://localhost:7000/


20180806205311610.png



jstack Java堆栈跟踪工具

概述



20180806205801143.png


语法及使用

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的主要选项

选项 作用
-F 当正常输出的请求不被响应时,强制输出线程堆栈
-m 如果调用到本地方法的话,可以显示C/C++的堆栈
-l 除了堆栈信息,显示关于锁的附件信息


用法举例

E:\Program Files\Java\jdk1.8.0_161\bin>jstack -l 12888
2018-08-06 21:01:10
Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.51-b03 mixed mode):
"DestroyJavaVM" prio=6 tid=0x000000000e1ab800 nid=0x2f80 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   Locked ownable synchronizers:
        - None
"DefaultQuartzScheduler_QuartzSchedulerThread" prio=6 tid=0x000000000e1a9800 nid=0xb5c in Object.wait() [0x000000000f59f000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:410)
        - locked <0x0000000762395018> (a java.lang.Object)
   Locked ownable synchronizers:
        - None
......
.......
..........


hsdis:jit生成代码反汇编

Sun官方推荐的HotSpot虚拟机JIT编译代码的反汇编插件。了解即可。

相关文章
|
2月前
|
Arthas 监控 Java
(十一)JVM成神路之性能调优篇:GC调优、Arthas工具详解及各场景下线上最佳配置推荐
“在当前的互联网开发模式下,系统访问量日涨、并发暴增、线上瓶颈等各种性能问题纷涌而至,性能优化成为了现时代开发过程中炙手可热的名词,无论是在开发、面试过程中,性能优化都是一个常谈常新的话题”。
101 3
|
26天前
|
缓存 监控 算法
吃透 JVM 诊断方法与工具使用
【8月更文挑战第4天】深入了解并掌握JVM诊断需把握几大要点:1) 熟悉JVM内存模型,如堆、栈及方法区;2) 掌握垃圾回收机制与算法;3) 运用工具如`jps`(查看Java进程)、`jstat`(监控运行状态)、`jmap`(生成堆快照)、`jhat`(分析堆快照)、`jstack`(检查线程栈); 4) 利用专业工具如Eclipse Memory Analyzer分析堆转储文件查找内存泄漏; 5) 动态监控与调整JVM参数; 6) 结合日志分析性能瓶颈。通过实战案例加深理解,有效应对JVM性能问题。
|
2月前
|
运维 监控 Java
(十)JVM成神路之线上故障排查、性能监控工具分析及各线上问题排错实战
经过前述九章的JVM知识学习后,咱们对于JVM的整体知识体系已经有了全面的认知。但前面的章节中,更多的是停留在理论上进行阐述,而本章节中则更多的会分析JVM的实战操作。
|
2月前
|
监控 算法 Java
深入探索Java虚拟机:性能监控与调优实践
在面对日益复杂的企业级应用时,Java虚拟机(JVM)的性能监控和调优显得尤为重要。本文将深入探讨JVM的内部机制,分析常见的性能瓶颈,并提供一系列针对性的调优策略。通过实际案例分析,我们将展示如何运用现代工具对JVM进行监控、诊断及优化,以提升Java应用的性能和稳定性。
|
24天前
|
Arthas Prometheus 监控
使用JDK自带工具调优JVM的常用命令
使用JDK自带工具调优JVM的常用命令
|
27天前
|
监控 Java Android开发
吃透 JVM 诊断方法与工具使用
【8月更文挑战第3天】要精通JVM诊断,需掌握关键监控指标如内存(堆/非堆)、CPU使用及线程状态;熟悉工具如`jstat`(监控状态)、`jmap`(堆转储)、`jstack`(线程堆栈);并能利用Eclipse Memory Analyzer (MAT)分析堆转储找内存泄漏;同时理解GC日志以优化垃圾回收行为;通过实践案例加深理解。
|
2月前
|
监控 Java 开发者
Java面试题:如何使用JVM工具(如jconsole, jstack, jmap)来分析内存使用情况?
Java面试题:如何使用JVM工具(如jconsole, jstack, jmap)来分析内存使用情况?
97 2
|
2月前
|
监控 算法 Java
怎么用JDK自带工具进行JVM内存分析
JVM内存分析工具,如`jps`、`jcmd`、`jstat`、`jstack`和`jmap`,是诊断和优化Java应用的关键工具。`jps`列出Java进程,`jcmd`执行诊断任务,如查看JVM参数和线程堆栈,`jstat`监控内存和GC,`jstack`生成线程堆栈信息,而`jmap`则用于生成堆转储文件。这些工具帮助排查内存泄漏、优化内存配置、性能调优和异常分析。例如,`jmap -dump:file=heapdump.hprof &lt;PID&gt;`生成堆转储文件,之后可以用Eclipse Memory Analyzer (MAT)等工具分析。
|
2月前
|
监控 负载均衡 Java
Java虚拟机调优技巧及性能监控
Java虚拟机调优技巧及性能监控
|
4月前
|
SQL 运维 监控
关系型数据库性能监控工具
【5月更文挑战第21天】
71 2