JDK自帶監控工具

 系統在生產運行過程當中最重要的工做莫過於監控與問題的處理,監控是預防問題產生很重要的手段。在監控過程當中能夠發現那些模塊或進程出現了問題,出現問題後會及時通知問題負責人。 javascript

        實現監控的手段很是多,有系統級別監控系統,也有監控小工具等等。Java 就已經自帶了一些監控工具,能夠不借助外部軟件的狀況下簡單、快速查看相應監控信息。 php

        這些監控工具存放在jdk/bin 目錄下,bin 目錄下有咱們熟悉的javac、java、rmic等編譯工具,如圖: html


 

        下面就分別介紹這些監控工具的使用及做用。 java

        1.jps 
        用來顯示本地的java進程,以及進程號,進程啓動的路徑等。 linux

        用法提示: windows

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

        jps實例: ruby

        (1)顯示運行中的Java進程: bash

jps #顯示結果: 8564 Jps
6844
1364 HelloWorld

        (2)顯示完整包名: 服務器

jps -l #顯示結果: 6844
8196 sun.tools.jps.Jps
1364 com.helloworld.hello.HelloWorld

        (3)輸出Java進程的命令行輸入參數: oracle

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 gcheap信息 -gccapacity:統計不一樣的generations(不知道怎麼翻譯好,包括新生區,老年區,permanent區)相應的heap容量狀況 -gccause:統計gc的狀況,(同-gcutil)和引發gc的事件 -gcnew:統計gc時,新生代的狀況 -gcnewcapacity:統計gc時,新生代heap容量 -gcold:統計gc時,老年區的狀況 -gcoldcapacity:統計gc時,老年區heap容量 -gcpermcapacity:統計gc時,permanentheap容量 -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.jvmstat 
        jvmstat是GUI版本jstat,由Java官方提供,目前最新版本爲3.0。

        官網地址: http://www.oracle.com/technetwork/java/jvmstat-142257.html

        下載以後解壓縮獲得如下目錄結構:

        目錄結構比較清晰,很容易就能分辨出各目錄的功能及做用: 
        bat:windows啓動程序

        bin:linux啓動程序

        docs:相關文檔

        etc:linux相關依賴庫

        jars:相關jar包 

        使用jvmstat以前須要配置相應環境變量,環境變量配置以下:

JVMSTAT_HOME:jvmstat安裝目錄 JVMSTAT_JAVA_HOME:JDK所在目錄,與JAVA_HOME值相同

        配置好兩個環境變量以後就能夠運行jvmstat了,運行命令爲:

visualgc pid #windows系統進入bat目錄 #linux系統進入bin目錄

        運行後界面自動打開並顯示相應內容,如圖所示:


  
 

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

        6.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實例:

        (1)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

        7.jconsole

        與前面提到的jvmstat相似的GUI監控程序,能夠以圖表化的形式顯示各類數據,並可經過遠程鏈接監視遠程的服務器VM,此類工具能夠較直觀觀察各類變化,但比較耗費資源。

jconsole

        界面以下:






  1. 如何使用JDK自帶jvisualvm監控工具

  2. 如何使用JDK自帶jvisualvm監控工具

  3. 如何使用JDK自帶jvisualvm監控工具
    如何使用JDK自帶jvisualvm監控工具
    如何使用JDK自帶jvisualvm監控工具

  4. 如何使用JDK自帶jvisualvm監控工具

  5. 如何使用JDK自帶jvisualvm監控工具

  6. 如何使用JDK自帶jvisualvm監控工具
    如何使用JDK自帶jvisualvm監控工具
相關文章
相關標籤/搜索