[Java性能剖析]Sun JDK基本性能剖析工具介紹

 Sun JDK隨版本同時發佈了一些性能剖析的工具,這些工具基本上都是基於JVM MangeAPI和Sun JVM Attach API實現,所以其能提供什麼樣的功能可以參見JVM Manage API 的說明。我們來了解一下:

    一、功能性工具
    1.jps:列出所有的JVM進程的進程ID和進程名字,實現原理可見Sun JVM Attach API
    2.jinfo:打印JVM啓動內部信息,譬如啓動參數、JVM版本、操作系統信息等,具體可參見JVM Manage API的OperatingSystem和Runtime這兩個Bean,本功能基於JVM Manage API實現。最常用的使用格式:

jinfo pid

     jinfo支持連接到遠程的JVM上(遠程服務需要啓動jsadebugd),實際應用意義不大
    3.jstat:一個內存和垃圾回收情況的統計工具,最常用的使用格式是:

jstat -outputOptions vmid [interval[s|ms] [count]]

    jstat支持連接到遠程的JVM上(遠程服務需要啓動jstatd),實際應用意義不大    
    可使用的outputOptions選項包括:

 
class Statistics on the behavior of the class loader.
compiler Statistics of the behavior of the HotSpot Just-in-Time compiler.
gc Statistics of the behavior of the garbage collected heap.
gccapacity Statistics of the capacities of the generations and their corresponding spaces.
gccause Summary of garbage collection statistics (same as -gcutil), with the cause of the last and current (if applicable) garbage collection events.
gcnew Statistics of the behavior of the new generation.
gcnewcapacity Statistics of the sizes of the new generations and its corresponding spaces.
gcold Statistics of the behavior of the old and permanent generations.
gcoldcapacity Statistics of the sizes of the old generation.
gcpermcapacity Statistics of the sizes of the permanent generation.
gcutil Summary of garbage collection statistics.
printcompilation HotSpot compilation method statistics.

     更詳細的說明見:http://java.sun.com/javase/6/docs/technotes/tools/share/jstat.html
     範例:

jstat -gc 23450 250 4 //23450是進程ID,250表示每250豪秒採樣一次,4是總執行4次,輸出結果如下:
S0C S1C S0U S1U EC EU OC OU PC PU YGC YGCT FGC FGCT GCT
1600.0 1600.0 786.5 0.0 13184.0 11964.2 114688.0 3684.3 131072.0 13484.2 8 0.127 0 0.000 0.127
1600.0 1600.0 786.5 0.0 13184.0 11964.2 114688.0 3684.3 131072.0 13484.2 8 0.127 0 0.000 0.127
1600.0 1600.0 786.5 0.0 13184.0 11964.2 114688.0 3684.3 131072.0 13484.2 8 0.127 0 0.000 0.127
1600.0 1600.0 786.5 0.0 13184.0 11964.2 114688.0 3684.3 131072.0 13484.2 8 0.127 0 0.000 0.127

S0C|S1C/ S0U|S1U:兩個Suvivor區容量/空閒的容量
EC/EU:Eden 區容量/空閒的容量
OC/OU:Tenured 區容量/空閒的容量
PC/PU:Perm區容量/空閒的容量
YGC/ YGCT:Young Gen回收次數/總回收時間
FGC/FGCT:Full GC次數/總回收時間
GCT:總的垃圾回收時間(YGCT+FGCT)

     4.jstack:打印JVM當前所有的線程、線程棧和鎖信息信息,最常用使用格式如下:

jstack –l pid

    範例:

jstack -l 23450
Full thread dump Java HotSpot(TM) Server VM (10.0-b22 mixed mode):

"Attach Listener" daemon prio=10 tid=0x08058400 nid=0x3005 waiting on condition [0x00000000..0x00000000]
java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
- None

"DestroyJavaVM" prio=10 tid=0x822af400 nid=0x5b9b waiting on condition [0x00000000..0xb7dc2120]
java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
- None
……

     jstack支持連接到遠程的JVM上(遠程服務需要啓動jsadebugd),實際應用意義不大
     5.jmap:當前的堆內存信息dump,JDK6中支持dump到一個文件,由其他分析程序對堆內存進行分析(譬如jhat/Eclipse Memory Anaylzer)。最常用的使用格式如下:

jmap -dump:file={filename} pid

     範例:如下將堆內存dump到result.bin中

jmap -dump:file=result.bin 23450
顯示如下信息:
Dumping heap to …/bin/result.bin ...
Heap dump file created

     二、輔助性工具
    輔助性工具本身沒有提供什麼功能,只是爲功能性工具提供支持
     1. jstatd/jsadebugd:提供遠程代理服務,支持jstat/jinfo/jstack通過遠程獲取信息,這個工具實際意義不大
     2.jhat:分析jmap dump出來的堆,以網頁的形式提供訪問。jhat功能基本上比較弱,非常佔用內存,在堆文件比較大(譬如超過1G)的情況下基本無法使用,Eclipse Memory Anaylzer是一個優秀的替代品。使用格式如下:

jhat {dumpfile}
在堆文件比較大的情況下,需要使用其他的啓動參數,譬如如下
jhat –J-mx512M {dumpfile}

    訪問http://localhost:7000可以看到分析後的結果
    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章