---
title: "JDK自带监控工具"
author: "g21121"
date: "2013-08-09T15:59:41Z"
tags: []
categories: ["编程语言"]
slug: "jdk"
draft: false
lastmod: "2016-05-14T17:42:59Z"
id: 1922630
url: "https://286.iteye.com/blog/1922630"
---

系统在生产运行过程中最重要的工作莫过于监控与问题的处理，监控是预防问题产生很重要的手段。在监控过程中可以发现那些模块或进程出现了问题，出现问题后会及时通知问题负责人。

        实现监控的手段非常多，有系统级别监控系统，也有监控小工具等等。Java 就已经自带了一些监控工具，可以不借助外部软件的情况下简单、快速查看相应监控信息。

        这些监控工具存放在jdk/bin 目录下，bin 目录下有我们熟悉的javac、java、rmic等编译工具，如图：

![](http://dl2.iteye.com/upload/attachment/0087/9891/3506b80b-5050-33e1-b12a-f0dc6da7a528.png)

 

        下面就分别介绍这些监控工具的使用及作用。

 

        **1.jps**  
        用来显示本地的java进程，以及进程号，进程启动的路径等。

        用法提示：

```
usage: jps [-help]
       jps [-q] [-mlvV] [<hostid>]

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

 

        ***jps实例：***

        (1)显示运行中的Java进程：

```
jps

#显示结果：
8564 Jps
6844
1364 HelloWorld
```

        (2)显示完整包名：

```
jps -l

#显示结果：
6844
8196 sun.tools.jps.Jps
1364 com.helloworld.hello.HelloWorld
```

        (3)输出Java进程的命令行输入参数：

```
jps -m

#显示结果：
8708 Jps -m
6844
1364 HelloWorld
```

        (4)显示相应Java进程的完整的JVM参数：

```
jps -v

#显示结果：
7296 Jps -Denv.class.path=.;C:\Program Files\Java\jdk1.6.0_38/lib/dt.jar;C:\Prog
ram Files\Java\jdk1.6.0_38/lib/tools.jar  -Dapplication.home=C:\Program Files\Ja
va\jdk1.6.0_38 -Xms8m
6844  -Xmx512m -XX:MaxPermSize=256m -XX:ReservedCodeCacheSize=64m
1364 HelloWorld -Dfile.encoding=UTF-8
```

 

        **2.jinfo**  
        jinfo可观察运行中java程序的运行环境参数，参数包括Java System属性和JVM命令行参数；也可从core文件里面知道崩溃的Java应用程序的配置信息。

        用法提示：

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

 

        ***jinfo实例：***

       首先，通过jps 命令获取Java程序HelloWorld 的进程号1364。（以下均以此为例）

        (1)显示所有与该进程相关的信息，如下所示：

```
jinfo 1364

#显示结果：
Attaching to process ID 1364, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 20.13-b02
Java System Properties:

java.runtime.name = Java(TM) SE Runtime Environment
sun.boot.library.path = C:\Program Files\Java\jdk1.6.0_38\jre\bin
java.vm.version = 20.13-b02
java.vm.vendor = Sun Microsystems Inc.

  ........(此处省略部分信息)

java.vendor = Sun Microsystems Inc.
file.separator = \
java.vendor.url.bug = http://java.sun.com/cgi-bin/bugreport.cgi
sun.io.unicode.encoding = UnicodeLittle
sun.cpu.endian = little
sun.desktop = windows
sun.cpu.isalist = amd64

VM Flags:

-Dfile.encoding=UTF-8
```

        (2)当然并不是所有信息都是我们需要的，还可以运行"jinfo -flag jvm参数 pid"显示相应jvm参数信息，例如：

```
jinfo -flag PermSize 1364

#显示结果：
-XX:PermSize=21757952


jinfo -flag MaxPermSize 1364

#显示结果：
-XX:MaxPermSize=134217728

jinfo -flag AllowUserSignalHandlers 1364

#显示结果：
-XX:-AllowUserSignalHandlers
```

        JVM 具体有哪些参数他们的意义都是什么可以参照我的另一篇文章。

 

        **3.jstat**

        jstat 利用了JVM 内建的指令对Java 应用程序的资源和性能进行实时的命令行的监控，包括了对Heap size 和垃圾回收状况的监控等。

        用法提示：

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

Definitions:
  <option>      选项，我们一般使用 -gcutil 查看gc情况
  <vmid>        VM的进程号，即当前运行的java进程号
  <lines>       Number of samples between header lines.
  <interval>    刷新间隔时间，单位为秒或者毫秒，
		格式：<n>["ms"|"s"]
                实例：1000ms/1s
  <count>       刷新次数，默认持续刷新
  -J<flag>      Pass <flag> directly to the runtime system.
```

 

        **常用option：**

```
-class：统计class loader行为信息

-compile：统计编译行为信息

-gc：统计jdk gc时heap信息

-gccapacity：统计不同的generations（不知道怎么翻译好，包括新生区，老年区，permanent区）相应的heap容量情况

-gccause：统计gc的情况，（同-gcutil）和引起gc的事件

-gcnew：统计gc时，新生代的情况

-gcnewcapacity：统计gc时，新生代heap容量

-gcold：统计gc时，老年区的情况

-gcoldcapacity：统计gc时，老年区heap容量

-gcpermcapacity：统计gc时，permanent区heap容量

-gcutil：统计gc时，heap情况
```

 

        ***jstat实例：***

        (1)jstat -gcutil

        统计gc heap情况，此选项是较常用的一个选项：

```
jstat -gcutil 1364

#显示结果：
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT
  0.00   0.00   8.00   0.00  26.07      0    0.000     0    0.000    0.000
```

        上面的命令没有刷新时间与刷新次数的参数，所以默认只打印出1行数据内容。  
        加入刷新间隔：1000ms或1s；刷新次数：5，得到如下结果：

```
jstat -gcutil 1364 1000ms/1s 5

#显示结果：
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT
  0.00   0.00   8.00   0.00  26.07      0    0.000     0    0.000    0.000
  0.00   0.00   8.00   0.00  26.07      0    0.000     0    0.000    0.000
  0.00   0.00   8.00   0.00  26.07      0    0.000     0    0.000    0.000
  0.00   0.00   8.00   0.00  26.07      0    0.000     0    0.000    0.000
  0.00   0.00   8.00   0.00  26.07      0    0.000     0    0.000    0.000

#参数含义：
S0  — Heap上的 Survivor space 0 区已使用空间的百分比
S1  — Heap上的 Survivor space 1 区已使用空间的百分比
E   — Heap上的 Eden space 区已使用空间的百分比
O   — Heap上的 Old space 区已使用空间的百分比
P   — Perm space 区已使用空间的百分比
YGC — 从应用程序启动到采样时发生 Young GC 的次数
YGCT– 从应用程序启动到采样时 Young GC 所用的时间(单位秒)
FGC — 从应用程序启动到采样时发生 Full GC 的次数
FGCT– 从应用程序启动到采样时 Full GC 所用的时间(单位秒)
GCT — 从应用程序启动到采样时用于垃圾回收的总时间(单位秒)
```

        当去掉次数限制后，控制台会不断刷新内容并一直显示。 

        (2)jstat -class  
        显示加载class的数量，及所占空间等信息：

```
jstat -class 1364

#显示结果：
Loaded  Bytes  Unloaded  Bytes     Time
   696  1351.4        0     0.0       0.21
```

        (3)jstat -compiler  
        显示VM实时编译的数量等信息：

```
jstat -compiler 1364

#显示结果：
Compiled Failed Invalid   Time   FailedType FailedMethod
      26      0       0     0.10          0
```

       (4)jstat –gccapacity  
        可以显示JVM内存中三代（young,old,perm）对象的使用和占用大小，如：PGCMN显示的是最小perm的内存使用量，PGCMX显示的是perm的内存最大使用量，PGC是当前新生成的perm内存占用量，PC是但前perm内存占用量。其他的可以根据这个类推， OC是old内存的占用大小。

```
jstat -gccapacity 1364

#显示结果：
 NGCMN    NGCMX     NGC     S0C   S1C       EC      OGCMN      OGCMX       OGC       OC      PGCMN    PGCMX     PGC       PC     YGC    FGC
 43584.0 697664.0  43584.0 5440.0 5440.0  32704.0    87168.0  1395392.0    87168.0    87168.0  21248.0  83968.0  21248.0  21248.0      0     0
```

 

        **4.jmap**  
        观察运行中的JVM 物理内存的占用情况，包括Heap size , Perm size 等。

        用法提示：

```
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                显示jvm heap的情况
    -histo[:live]        显示jvm heap的直方图; 如果使用"live"参数则只显示存活对象的情况
    -permstat            显示permanent generation heap情况
    -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实例：***

        (1)jmap pid

        显示运行中Java进程：

```
jmap 1364

#显示结果：
Attaching to process ID 1364, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 20.13-b02
0x0000000000400000      188K    C:\Program Files\Java\jdk1.6.0_38\bin\javaw.exe
0x000000006d450000      156K    C:\Program Files\Java\jdk1.6.0_38\jre\bin\java.dll
0x000000006d830000      56K     C:\Program Files\Java\jdk1.6.0_38\jre\bin\verify.dll
0x000000006d880000      72K     C:\Program Files\Java\jdk1.6.0_38\jre\bin\zip.dll
0x000000006d8c0000      7916K   C:\Program Files\Java\jdk1.6.0_38\jre\bin\server\jvm.dll
0x00000000712e0000      308K    D:\Program Files\AVAST Software\Avast\snxhk64.dll
0x0000000077640000      1000K   C:\Windows\system32\USER32.dll
0x0000000077740000      1148K   C:\Windows\system32\KERNEL32.dll
0x0000000077860000      1700K   C:\Windows\SYSTEM32\ntdll.dll
0x000007fefb6d0000      236K    C:\Windows\system32\WINMM.dll
0x000007fefd6f0000      428K    C:\Windows\system32\KERNELBASE.dll
0x000007fefda70000      124K    C:\Windows\SYSTEM32\sechost.dll
0x000007fefda90000      412K    C:\Windows\system32\GDI32.dll
0x000007fefde40000      804K    C:\Windows\system32\USP10.dll
0x000007fefe3d0000      876K    C:\Windows\system32\ADVAPI32.dll
0x000007fefe590000      184K    C:\Windows\system32\IMM32.DLL
0x000007fefe640000      1060K   C:\Windows\system32\MSCTF.dll
0x000007fefe7d0000      56K     C:\Windows\system32\LPK.dll
0x000007fefe7e0000      1204K   C:\Windows\system32\RPCRT4.dll
0x000007fefeb80000      636K    C:\Windows\system32\msvcrt.dll 
```

        (2) jmap -heap

        显示JVM HEAP情况：

```
jmap -heap 1364

#显示结果：
Attaching to process ID 1364, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 20.13-b02

using thread-local object allocation.
Parallel GC with 4 thread(s)

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 2143289344 (2044.0MB)
   NewSize          = 1310720 (1.25MB)
   MaxNewSize       = 17592186044415 MB
   OldSize          = 5439488 (5.1875MB)
   NewRatio         = 2
   SurvivorRatio    = 8
   PermSize         = 21757952 (20.75MB)
   MaxPermSize      = 85983232 (82.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 33488896 (31.9375MB)
   used     = 669792 (0.638763427734375MB)
   free     = 32819104 (31.298736572265625MB)
   2.0000420437866926% used
From Space:
   capacity = 5570560 (5.3125MB)
   used     = 0 (0.0MB)
   free     = 5570560 (5.3125MB)
   0.0% used
To Space:
   capacity = 5570560 (5.3125MB)
   used     = 0 (0.0MB)
   free     = 5570560 (5.3125MB)
   0.0% used
PS Old Generation
   capacity = 89260032 (85.125MB)
   used     = 0 (0.0MB)
   free     = 89260032 (85.125MB)
   0.0% used
PS Perm Generation
   capacity = 21757952 (20.75MB)
   used     = 3071056 (2.9287872314453125MB)
   free     = 18686896 (17.821212768554688MB)
   14.11463725997741% used
```

        (3) jmap -histo

        显示JVM资源直方图：

```
jmap -histo 1364
#将结果输出至jmap.log文件中，这样做的好处是方便查看
jmap -histo pid>jmap.log

#显示结果：
 num     #instances         #bytes  class name
----------------------------------------------
   1:        382046       18338208  java.nio.HeapCharBuffer
   2:          4914         673360  <methodKlass>
   3:          4914         609488  <constMethodKlass>
   4:          8256         512216  <symbolKlass>
   5:           406         484728  [I
   6:           327         377520  <constantPoolKlass>
   7:           300         255328  <constantPoolCacheKlass>
   8:           327         237800  <instanceKlassKlass>
   9:          1606         208208  [C

	......(省略部分内容)

 162:             1             16  java.lang.String$CaseInsensitiveComparator
 163:             1             16  sun.misc.Launcher
 164:             1             16  java.util.Collections$EmptyList
 165:             1             16  java.util.Hashtable$EmptyIterator
 166:             1             16  java.io.File$1
 167:             1             16  java.util.Collections$ReverseComparator
Total        408629       22067640
```

        (4)jmap -histo:live

        显示活动状态对象JVM资源直方图：

```
jmap -histo:live 1364

#显示结果：
 num     #instances         #bytes  class name
----------------------------------------------
   1:          4896         670912  <methodKlass>
   2:          4896         607584  <constMethodKlass>
   3:          8232         511056  <symbolKlass>
   4:           326         376192  <constantPoolKlass>
   5:           300         255392  <constantPoolCacheKlass>
   6:           326         237216  <instanceKlassKlass>
   7:          1576         206720  [C
   8:           393          77144  [B
   9:          1583          50656  java.lang.String
  10:           396          41184  java.lang.Class
  
	......(省略部分内容)

 161:             1             16  java.net.UnknownContentHandler
 162:             1             16  [Ljava.lang.StackTraceElement;
 163:             1             16  sun.misc.Launcher
 164:             1             16  java.util.Collections$EmptyList
 165:             1             16  java.util.Hashtable$EmptyIterator
 166:             1             16  java.util.Collections$ReverseComparator
 167:             1             16  java.nio.charset.CoderResult$1
Total         26383        3235456
```

        (5)jmap -dump:live,format=b,file=heap.bin pid

        将dump信息保存至heap.bin中

```
jmap -dump:live,format=b,file=heap.bin 1346
```

 

         **5.jstack**

        可以观察到jvm中当前所有线程的运行情况和线程当前状态。

        用法提示：

```
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实例：***

        jstack pid

```
jstack 7644
2013-08-09 15:38:59
Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.13-b02 mixed mode):

"Low Memory Detector" daemon prio=6 tid=0x0000000006adb800 nid=0x18a4 runnable [
0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" daemon prio=10 tid=0x0000000006ad0800 nid=0x1e34 waiting on
 condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" daemon prio=10 tid=0x0000000006a85000 nid=0x1e70 waiting on
 condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

.......(省略部分内容)

JNI global references: 882
```