java8的jps,jstat,jstack,jmp,jinfo,jhat使用详解及Java自带可视化监控与故障分析工具的介绍

概述

JDK提供的免费的JVM监控与故障处理工具有:

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

这几种工具在官方文档中都有详细的说明,而且都被标注为 
experimental and unsupported,官方将来主力发展的将是多合一故障处理工具VisualVM”VisualVM的功能非常的强大,可以基于插件扩展各种功能,覆盖了上边所列出来的所有工具的功能,而且图像化界面更加的清晰明了。
对于习惯了在linux非桌面等环境种工作的人员,让可以使用以上命令行工具进行jvm监控和故障分析,下面就列出来以上各命令行工具的具体用法以供参考

jps: 虚拟机进程状况工具

命令格式

jps [options] [hostid]

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

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

命令格式:

 jstat [option  vmid  [interval [s|ms]   [count]]  ]

对于命令格式中的
VMID
LVMID需要特别说明一下:如果是本地虚拟机进程,VMID与LVMID是一致的,如果是远程虚拟机进程,那VMID的格式应当是:
[protocol:][//]lvmid[@hostname[:port]/servername]

interval 代表查询的间隔, count 代表查询的次数



选项option 主要分为3类:类装载,垃圾收集,运行期编译状况。

class: Displays statistics about the behavior of the class loader.

compiler: Displays statistics about the behavior of the Java HotSpot VM Just-in-Time compiler.

gc: Displays statistics about the behavior of the garbage collected heap.

gccapacity: Displays statistics about the capacities of the generations and their corresponding spaces.

gccause: Displays a summary about garbage collection statistics (same as -gcutil), with the cause of the last and current (when applicable) garbage collection events.

gcnew: Displays statistics of the behavior of the new generation.

gcnewcapacity: Displays statistics about the sizes of the new generations and its corresponding spaces.

gcold: Displays statistics about the behavior of the old generation and metaspace statistics.

gcoldcapacity: Displays statistics about the sizes of the old generation.

gcmetacapacity: Displays statistics about the sizes of the metaspace.

gcutil: Displays a summary about garbage collection statistics.

printcompilation: Displays Java HotSpot VM compilation method statistics.

jdk8 jstat 选项的输出说明

-class 
option

Class loader statistics.

Loaded: Number of classes loaded.

Bytes: Number of kBs loaded.

Unloaded: Number of classes unloaded.

Bytes: Number of Kbytes unloaded.

Time: Time spent performing class loading and unloading operations.

-compiler 
option

Java HotSpot VM Just-in-Time compiler statistics.

Compiled: Number of compilation tasks performed.

Failed: Number of compilations tasks failed.

Invalid: Number of compilation tasks that were invalidated.

Time: Time spent performing compilation tasks.

FailedType: Compile type of the last failed compilation.

FailedMethod: Class name and method of the last failed compilation.

-gc 
option

Garbage-collected heap statistics.

S0C: Current survivor space 0 capacity (kB).

S1C: Current survivor space 1 capacity (kB).

S0U: Survivor space 0 utilization (kB).

S1U: Survivor space 1 utilization (kB).

EC: Current eden space capacity (kB).

EU: Eden space utilization (kB).

OC: Current old space capacity (kB).

OU: Old space utilization (kB).

MC: Metaspace capacity (kB).

MU: Metacspace utilization (kB).

CCSC: Compressed class space capacity (kB).

CCSU: Compressed class space used (kB).

YGC: Number of young generation garbage collection events.

YGCT: Young generation garbage collection time.

FGC: Number of full GC events.

FGCT: Full garbage collection time.

GCT: Total garbage collection time.

-gccapacity 
option

Memory pool generation and space capacities.

NGCMN: Minimum new generation capacity (kB).

NGCMX: Maximum new generation capacity (kB).

NGC: Current new generation capacity (kB).

S0C: Current survivor space 0 capacity (kB).

S1C: Current survivor space 1 capacity (kB).

EC: Current eden space capacity (kB).

OGCMN: Minimum old generation capacity (kB).

OGCMX: Maximum old generation capacity (kB).

OGC: Current old generation capacity (kB).

OC: Current old space capacity (kB).

MCMN: Minimum metaspace capacity (kB).

MCMX: Maximum metaspace capacity (kB).

MC: Metaspace capacity (kB).

CCSMN: Compressed class space minimum capacity (kB).

CCSMX: Compressed class space maximum capacity (kB).

CCSC: Compressed class space capacity (kB).

YGC: Number of young generation GC events.

FGC: Number of full GC events.

-gccause 
option

This option displays the same summary of garbage collection statistics as the -gcutil option, but includes the causes of the last garbage collection event and (when applicable) the current garbage collection event. In addition to the columns listed for -gcutil, this option adds the following columns.

LGCC: Cause of last garbage collection

GCC: Cause of current garbage collection

-gcnew 
option

New generation statistics.

S0C: Current survivor space 0 capacity (kB).

S1C: Current survivor space 1 capacity (kB).

S0U: Survivor space 0 utilization (kB).

S1U: Survivor space 1 utilization (kB).

TT: Tenuring threshold.

MTT: Maximum tenuring threshold.

DSS: Desired survivor size (kB).

EC: Current eden space capacity (kB).

EU: Eden space utilization (kB).

YGC: Number of young generation GC events.

YGCT: Young generation garbage collection time.

-gcnewcapacity 
option

New generation space size statistics.

NGCMN: Minimum new generation capacity (kB).

NGCMX: Maximum new generation capacity (kB).

NGC: Current new generation capacity (kB).

S0CMX: Maximum survivor space 0 capacity (kB).

S0C: Current survivor space 0 capacity (kB).

S1CMX: Maximum survivor space 1 capacity (kB).

S1C: Current survivor space 1 capacity (kB).

ECMX: Maximum eden space capacity (kB).

EC: Current eden space capacity (kB).

YGC: Number of young generation GC events.

FGC: Number of full GC events.

-gcold 
option

Old generation and metaspace behavior statistics.

MC: Metaspace capacity (kB).

MU: Metaspace utilization (kB).

CCSC: Compressed class space capacity (kB).

CCSU: Compressed class space used (kB).

OC: Current old space capacity (kB).

OU: Old space utilization (kB).

YGC: Number of young generation GC events.

FGC: Number of full GC events.

FGCT: Full garbage collection time.

GCT: Total garbage collection time.

-gcoldcapacity 
option

Old generation size statistics.

OGCMN: Minimum old generation capacity (kB).

OGCMX: Maximum old generation capacity (kB).

OGC: Current old generation capacity (kB).

OC: Current old space capacity (kB).

YGC: Number of young generation GC events.

FGC: Number of full GC events.

FGCT: Full garbage collection time.

GCT: Total garbage collection time.

-gcmetacapacity 
option

Metaspace size statistics.

MCMN: Minimum metaspace capacity (kB).

MCMX: Maximum metaspace capacity (kB).

MC: Metaspace capacity (kB).

CCSMN: Compressed class space minimum capacity (kB).

CCSMX: Compressed class space maximum capacity (kB).

YGC: Number of young generation GC events.

FGC: Number of full GC events.

FGCT: Full garbage collection time.

GCT: Total garbage collection time.

-gcutil 
option

Summary of garbage collection statistics.

S0: Survivor space 0 utilization as a percentage of the space’s current capacity.

S1: Survivor space 1 utilization as a percentage of the space’s current capacity.

E: Eden space utilization as a percentage of the space’s current capacity.

O: Old space utilization as a percentage of the space’s current capacity.

M: Metaspace utilization as a percentage of the space’s current capacity.

CCS: Compressed class space utilization as a percentage.

YGC: Number of young generation GC events.

YGCT: Young generation garbage collection time.

FGC: Number of full GC events.

FGCT: Full garbage collection time.

GCT: Total garbage collection time.

-printcompilation 
option

Java HotSpot VM compiler method statistics.

Compiled: Number of compilation tasks performed by the most recently compiled method.

Size: Number of bytes of byte code of the most recently compiled method.

Type: Compilation type of the most recently compiled method.

Method: Class name and method name identifying the most recently compiled method. Class name uses slash (/) instead of dot (.) as a name space separator. Method name is the method within the specified class. The format for these two fields is consistent with the HotSpot -XX:+PrintCompilation option.

详情参考官方文档 
http://docs.oracle.com/javase/8/docs/technotes/tools/unix/jstat.html

jinfo:java配置信息工具

实时地查看和调整虚拟机各项参数。

命令格式:

jinfo [ option ] pid

jinfo [ option ] executable core

jinfo [ option ] [ servier-id ] remote-hostname-or-IP

选项:

no-option

Prints both command-line flags and system property name-value pairs.

-flag 
name

Prints the name and value of the specified command-line flag.

-flag 
[+|-]name

enables or disables the specified Boolean command-line flag.

-flag 
name=value

Sets the specified command-line flag to the specified value.

-flags

Prints command-line flags passed to the JVM.

-sysprops

Prints Java system properties as name-value pairs.

-h

Prints a help message.

-help

Prints a help message.

参数说明:

pid

The process ID for which the configuration information is to be printed. The process must be a Java process. To get a list of Java processes running on a machine, use the jps(1) command.

executable

The Java executable from which the core dump was produced.

core

The core file for which the configuration information is to be printed.

remote-hostname-or-IP

The remote debug server hostname or IP address. See jsadebugd(1).

server-id

An optional unique ID to use when multiple debug servers are running on the same remote host.

jmap:Java内存映像工具

命令格式:

jmap [ options ] pid

jmap [ options ] executable core

jmap [ options ] [ pid ] server-id@ ] remote-hostname-or-IP

选项:

<no option>

When no option is used, the jmap command prints shared object mappings. For each shared object loaded in the target JVM, the start address, size of the mapping, and the full path of the shared object file are printed. This behavior is similar to the Oracle Solaris pmap utility.

-dump:[live,] format=b, file=
filename

Dumps the Java heap in hprof binary format to filename. The live suboption is optional, but when specified, only the active objects in the heap are dumped. To browse the heap dump, you can use the jhat(1) command to read the generated file.

-finalizerinfo

Prints information about objects that are awaiting finalization.

-heap

Prints a heap summary of the garbage collection used, the head configuration, and generation-wise heap usage. In addition, the number and size of interned Strings are printed.

-histo[:live]

Prints a histogram of the heap. For each Java class, the number of objects, memory size in bytes, and the fully qualified class names are printed. The JVM internal class names are printed with an asterisk (*) prefix. If the live suboption is specified, then only active objects are counted.

-clstats

Prints class loader wise statistics of Java heap. For each class loader, its name, how active it is, address, parent class loader, and the number and size of classes it has loaded are printed.

-F

Force. Use this option with the jmap -dump or jmap -histo option when the pid does not respond. The live suboption is not supported in this mode.

-h

Prints a help message.

-help

Prints a help message.

参数说明:

pid

The process ID for which the memory map is to be printed. The process must be a Java process. To get a list of Java processes running on a machine, use the jps(1) command.

executable

The Java executable from which the core dump was produced.

core

The core file for which the memory map is to be printed.

remote-hostname-or-IP

The remote debug server hostname or IP address. See jsadebugd(1).

server-id

An optional unique ID to use when multiple debug servers are running on the same remote host.

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

命令格式:

jhat [ options ] heap-dump-file

选项:

-stack false|true

Turns off tracking object allocation call stack. If allocation site information is not available in the heap dump, then you have to set this flag to false. The default is true.

-refs false|true

Turns off tracking of references to objects. Default is true. By default, back pointers, which are objects that point to a specified object such as referrers or incoming references, are calculated for all objects in the heap.

-port 
port-number

Sets the port for the jhat HTTP server. Default is 7000.

-exclude 
exclude-file

Specifies a file that lists data members that should be excluded from the reachable objects query. For example, if the file lists java.lang.String.value, then, then whenever the list of objects that are reachable from a specific object o are calculated, reference paths that involve java.lang.String.value field are not considered.

-baseline 
exclude-file

Specifies a baseline heap dump. Objects in both heap dumps with the same object ID are marked as not being new. Other objects are marked as new. This is useful for comparing two different heap dumps.

-debug 
int

Sets the debug level for this tool. A level of 0 means no debug output. Set higher values for more verbose modes.

-version

Reports the release number and exits

-h

Displays a help message and exits.

-help

Displays a help message and exits.

-J
flag

Passes flag to the Java Virtual Machine on which the jhat command is running. For example, -J-Xmx512m to use a maximum heap size of 512 MB.

参数说明:

heap-dump-file

Java binary heap dump file to be browsed. For a dump file that contains multiple heap dumps, you can specify which dump in the file by appending #<number> to the file name, for example, myfile.hprof#3.

jstack:Java堆栈跟踪工具

命令格式:

jstack [ options ] pid

jstack [ options ] executable core

jstack [ options ] [ server-id@ ] remote-hostname-or-IP

选项:

-F

Force a stack dump when jstack [-lpid does not respond.

-l

          Long listing. Prints additional information about locks such as a list of owned java.util.concurrent ownable synchronizers. See the AbstractOwnableSynchronizer class description at  http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/AbstractOwnableSynchronizer.html

-m

Prints a mixed mode stack trace that has both Java and native C/C++ frames.

-h

Prints a help message.

-help

Prints a help message.

参数说明:

pid

The process ID for which the stack trace is printed. The process must be a Java process. To get a list of Java processes running on a machine, use the jps(1) command.

executable

The Java executable from which the core dump was produced.

core

The core file for which the stack trace is to be printed.

remote-hostname-or-IP

The remote debug server hostname or IP address. See jsadebugd(1).

server-id

An optional unique ID to use when multiple debug servers are running on the same remote host.

JConsole:Java监视与管理控制台

是一种基于JMX的可视化监视,管理工具可查看内存,线程,类等信息。

《java8的jps,jstat,jstack,jmp,jinfo,jhat使用详解及Java自带可视化监控与故障分析工具的介绍》

VisualVM:多合一故障处理工具

VisualVM是到目前为止随JDK发布的功能最强大的运行监视和故障处理程序,在未来,也是官方主力发展的工具。它处理运行监视,故障处理外,还提供了性能分析等很多其他方面的功能。VisualVM的性能分析功能甚至比起JProfiler,YourKit等专业且收费的Profiling工具都不会逊色多少。

VisualVM基于NetBeans平台开发,一开始就具备了插件扩展功能的特性,通过插件扩展支持,VisualVM可以做到

  1. 显示虚拟机进程以及进程的配置,环境信息(jps, jinfo
  2. 监视应用程序的CPU,GC,堆,方法区以及线程的信息(jstat,jstack
  3. dump以及分析堆转储快照(jmap,jhat
  4. 方法级的程序运行性能分析,找出被调用最多,运行时间最长的方法
  5. 离线程序快照:收集程序的运行时配置,线程dump,内存dump等信息建立一个快照,可以将快照发送开发者处进行Bug反馈
  6. 其他plugins的无限的可能性……

《java8的jps,jstat,jstack,jmp,jinfo,jhat使用详解及Java自带可视化监控与故障分析工具的介绍》

    原文作者:烦啦
    原文地址: https://blog.csdn.net/A_zhenzhen/article/details/77962376
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞