虚拟机监控和分析工具

来源:互联网 发布:素描大师软件下载 编辑:程序博客网 时间:2024/06/04 19:10

背景:
给一个系统定位问题的时候,知识、经验是关键基础,数据是依据,工具是运用知识处理数据的手段。数据包括:运行日志,异常堆栈、GC日志、线程快照(threaddump/javacore文件)、堆转储快照(humpdump/hprof文件)。定位问题的时候,恰当使用虚拟机监控和分析工具可以起到事半功倍的效果。


1、JDK的命令行工具

JDK的bin目录下java.exejavac.exejps:JVM Process Status Tool,显示指定系统内所以HotSpot虚拟机进程jstat: JVM Statistics Monitoring Tool,用于收集HotSpot虚拟机各方面的运行数据jinfo:Configuration Info for Java,显示虚拟机配置信息jmap:Memory for Java,生成虚拟机的内存转储快照(heapdump文件)jhat:JVM Heap Dump Browser,用于分析heapdump文件,它会建立一个HTTP/HTML服务器,让用户可以在浏览器上查看分析结果jstack:Stack Trace for Java,显示虚拟机的线程快照

2、JDK监控和分析工具详解

jps概述:
类似Linux的ps命令,可以列出正在运行的虚拟机进程、显示虚拟机执行主类(Main Class,main()函数所在的类)名称以及这些进程的本地虚拟机唯一ID(Local Virtual Machine Identifier,LVMID)

jps命令格式:

jps[options][hostid]

jps执行样列:

C:\Users\Administrator>jps -l73043872 sun.tools.jps.Jps

jps工具主要选项

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

===============================

jstat:虚拟机统计信息监视工具
监视虚拟机各种运行状态信息的命令行工具,显示本地或远程虚拟机中的类装载、内存、垃圾收集、JIT编译等运行数据。它是运行期定位虚拟机性能问题的首先工具。

jstat命令格式

jstat[option vmid[interval [s|ms][count]]]参数:VMID与LVMID的区别,如果是本地虚拟机进程,VMID与LVMID是一致的,如果是远程虚拟机,那么VMID的格式为:[protocol:][//]lvmid[@hostname[:port]/servername]interval:查询间隔,可省略count:查询次数,可省略

jstat执行样列
样列1

jstat -gc 2764 250 20每隔250毫秒查询一次进程2764垃圾收集状况,一共查询20次

样列2

C:\Users\Administrator>jstat -gcutil 7304  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT  0.00   0.00   6.18  43.31  99.76    126    2.227   117   37.535   39.762

解释:
新生代Eden区(E,表示Eden)使用了6.18%的空间,两个Survivor(S0,S1表示Survivor0、Survivor1)里面都是空的,老年代(O,表示Old)和永久代(P,表示Permanent)则分别使用了43.31%和99.76%的空间。程序运行以来共发生Minor GC(YGC,表示Young GC)共126次,总共耗时2.227秒,发生Full GC(FGC,表示Full GC)117次,Full GC总耗时(FGCT,表示Full GC Time)为37.535秒,所以GC总耗时(GCT,表示GC Time)为39.762秒。

C:\Users\Administrator>jinfoUsage:    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 messageC:\Users\Administrator>jstatinvalid argument countUsage: 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.

jstat工具主要选项

选项 作用 -class 监视类装载、卸载数量、总空间以及类装载所耗费的时间 -gc 监视Java堆状况,包括Eden区、两个Survivor区、老年代、永久代等的容量、已用空间、GC时间合计等信息 -gccapacity 监视内容与-gc基本相同,但输出主要关注Java堆各个区域使用到的最大、最小空间 -gcutil 监视内容与-gc基本相同,但输出主要关注已使用空间占总空间的百分比 -gccause 与-gcutil功能一样,但是会额外输出导致上一次GC产生的原因 -gcnew 监视新生代GC状况 -gcnewcapacity 监视内容与-gcnew基本相同,输出主要关注使用到的最大、最小空间 -gcold 监视老年代GC状况 -gcoldcapacity 监视内容与-gcold基本相同,输出主要关注使用到的最大、最小空间 -gcpermcapacity 输出永久代使用到的最大、最小空间 -compiler 输出JIT编译器编译过的方法、耗时等信息 -printcompilation 输出已经被JIT编译的方法


==================================

jinfo:Java配置信息工具
实时查看和调整虚拟机各项参数。
使用jps命令的-v参数可以查看虚拟机启动时显示指定的参数列表,
但如果想知道未被显示指定的参数的系统默认值,可以使用jinfo的-flag选项进行查询。
此外,jinfo的-sysprops选项把虚拟机进程的System.getProperties()的内容打印出来。

jinfo命令格式:

jinfo[option]pid

执行样列:

C:\Users\Administrator>jinfo -flag  CMSIntiatingOccupancyFraction 1444查询CMSIntiatingOccupancyFraction参数值
C:\Users\Administrator>jinfoUsage:    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

==================================

jmap:Java内存映像工具
生成堆转储快照(一般称为humpdump文件或dump文件)
查询finalize执行队列、Java堆和永久代的详细信息,如空间使用率、当前使用的是哪种收集器等。

jmap命令格式

jmap[option]vmid

jmap工具主要选项option

选项 作用 -dump 生成Java堆存储快照。 -finalizerinfo -heap 显示Java堆详细信息 -histo 显示堆中对象统计信息 -permstat -F


使用样列:

C:\Users\Administrator>jmap -dump: format=b,file=eclipse.bin 6844
C:\Users\Administrator> jmapUsage:    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    -permstat            to print permanent generation 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
原创粉丝点击