JVM 自带工具介绍

JDK 的自带工具所在目录,相信大家都已经很清楚了:windows版本:jdk\bin,具体的实现是jdk\lib\tools.jar中,有兴趣的朋友可以看看,在本文主要介绍一下几个比较常用的工具:

                                                           SUN JDK 监控和故障处理工具

JVM 自带工具介绍

1、jps :虚拟机进程状况工具

功能:

     列出正在运行的虚拟机进程,并显示虚拟机执行主类(Main Class ,main方法所在的类)名称以及这些进程的本地虚拟机唯一ID(Local Virtual Machine Identifier,LVMID)。

命令格式:

     jsp [ options ] [ hostid ]  (jps [-q] [-mlvV] [<hostid>])

JVM 自带工具介绍

2、jstat:虚拟机统计信息监控工具

功能:

      用于监控虚拟机各种运行状态信息的命令行工具,可以显示本地或者远程虚拟机进程中的类装载、内存、垃圾收集、JIT编译等运行数据,在没有GUI图形界面,只提供了纯文本控制台环境的服务器上,它将是运行期定位虚拟机问题的首选工具。

命令格式:

     jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]

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

   参数interval和count代表查询间隔和次数,如果省略这两个参数,说明只查询一次。假设需要每100毫秒查询一次进程2345垃圾收集状况,一共查询30次,那么命令应该是:jstat -gc 2345 100 30

   选项option代表着用户希望查询的虚拟机信息,主要分为三类:类装载、垃圾收集、运行期编译状况:JVM 自带工具介绍

3、jinfo:Java虚拟机配置信息工具

功能:

     实时查看和调整虚拟机各项参数。使用jps命令的-v参数可以查看虚拟机启动时显示指定的参数列表,但如果想知道未被显示指定的参数的系统默认值可以用jinfo的-flag选项进行查询。当然也可以使用参数-XX:+PrintFlagsFinal 打印所有的参数。

命令格式:

    jinfo [option] <vmid>

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

4、jmap:Java内存映射工具( Memory Map for java )

 功能:

      用于生成对转储快照(一般称为heapdump或者dump文件),同时也可以通过设置JVM参数获取Java对转储文件 参数-XX:+HeapDumpOnOutOfMemoryError可以让虚拟机在出现OOM 的时候生成Dump文件 。同时jmap还可以查询finalize执行队列、Java堆和永久代的详细信息如空间使用率,当前使用的是哪种垃圾收集器。

命令格式:

     jmap [option] <pid>

  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.


JVM 自带工具介绍


5、jhat: 虚拟机堆转储快照分析工具(JVM Heap Analysis Tool)

功能:

        该工具与jmap搭配使用,来分析jmap 生成的对转储快照。jhat 工具内置了一个微型的HTTP/HTML服务器,生成dump的分析结果后可以通过浏览器中查看。

命令格式: 

         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
        -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

 6、jstack : Java堆栈跟踪工具(Stack  Trace for Java )

功能:

     用于生成虚拟机当前时刻的线程快照(一般称为Threaddump或者javacore文件)。线程快照是指当前虚拟机内每一条线程正在执行的方法堆栈的集合。生成线程快照的主要目的是定位出现长时间停顿的原因,如线程间死锁、死循环、请求外部资源导致的长时间等待。

命令格式:

      jstack -F [-m] [-l] <pid>

  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