標籤:
在 java 的 bin 目錄下,jdk 提供了很多使用的工具,下面學習一些監控和故障處理的工具。
名稱
作用
jps JVM process status tool,顯示指定系統內所有的 HotSpot 虛擬機器進程
jstat JVM statistics monitoring tool,用於收集 HotSpot 虛擬機器各方面的運行資料
jinfo 顯示虛擬機器設定資訊
jmap 生產虛擬機器的記憶體快照 dump 檔案
jhat 分析 dump 檔案
jstack 顯示虛擬機器的線程快照
jps 虛擬機器進程狀況工具
jps 的命令格式:
jps [options] [hostid]
1
2
3
4
5
6
7
>jps -l
25330 sun.tools.jps.Jps
25296
>jps -lv
25356 sun.tools.jps.Jps -Dapplication.home=/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home -Xms8m
25296 -Dosgi.requiredJavaVersion=1.6 -XstartOnFirstThread -Dorg.<span class="wp_keywordlink"><a href="http://res.importnew.com/eclipse" title="Eclipse ImportNew首頁" target="_blank">Eclipse</a></span>.swt.internal.carbon.smallFonts -XX:MaxPermSize=256m -Xms40m -Xmx512m -Xdock:icon=../Resources/Eclipse.icns -XstartOnFirstThread -Dorg.eclipse.swt.internal.carbon.smallFonts
jps 可以查看通過 rmi 協議查詢開啟了 rmi 服務的原創虛擬機器進程狀態,hostid 是 rmi 註冊表中註冊的主機。
jps 有如下主要的選項:
選項
作用
-q 只輸出 LVMID,省略主類的名稱
-m 輸出虛擬機器啟動時候傳遞給 main 方法的參數
-l 輸出類的全名
-v 輸出虛擬機器進程啟動時 JVM 參數
jstat 虛擬機器統計資訊監視工具
jstat 可以顯示本地或者遠程虛擬機器進程中的類裝載、記憶體、垃圾收集、 JIT 編譯等運行資料。
jstat 的命令格式:
jstat [option vmid [interval] [count]]
例如:
1
2
3
4
5
6
7
>jstat -gcutil 25296 1000 5
S0 S1 E O P YGC YGCT FGC FGCT GCT
0.00 99.54 90.43 93.70 95.23 55 1.156 5 1.990 3.146
0.00 99.54 90.43 93.70 95.23 55 1.156 5 1.990 3.146
0.00 99.54 90.43 93.70 95.23 55 1.156 5 1.990 3.146
0.00 99.54 90.43 93.70 95.23 55 1.156 5 1.990 3.146
0.00 99.54 90.43 93.70 95.23 55 1.156 5 1.990 3.146
查詢 25296 進程的虛擬機器狀況,並且每隔 1000 毫秒一次,顯示 5 次。
看下主要選項的含義:
選項
作用
-class 監視類裝載、卸載數量、總看見以及類裝載消耗的時間
-gc 監視 java 堆狀況,包括 eden 區、兩個 survivor 區、年老代、永久代等的容量、已用空間、gc 時間合計等
-gccapacity 內容與 -gc 基本相同,輸出主要關注 java 堆各個區使用到的最大、最小空間
-gcutil 內容與 -gc 基本相同,關注已使用地區佔總空間的百分比
-gccause 內容與 -gcutil 一樣,並且多輸出導致上一次 gc 產生的原因
-gcnew 監視新生代狀況
-gcnewcapacity 與 -gcnew 相同,主要關注使用到的最大、最小空間
-compiler 輸出 JIT 編譯器編譯過的方法、耗時等資訊
下面解讀下 -gcutil 所產生的內容:
S0、S1 分別代表了 Survivor0 和 Survivor1,E 代表 Eden 區,O 代表老年區, P 代表永久代。YGC 代表 Young GC 的次數,YGCT 代表時間,後面一樣解釋。
jinfo 查看 java 配置資訊工具
這個命令比較簡單,直接看內建的描述:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Usage:
jinfo [option] <pid>
(to connect to running process)
jinfo [option] <executable <core>
(to connect to a core file)
jinfo [option] [[email protected]]<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
jmap 生產 java 記憶體 dump
jmap 除了可以產生 dump 檔案外,還可以查詢 finalize 執行隊列,java 堆和永久代的詳細資料,如空間使用率和當前用的是哪種收集器等。
具體的 jamp 如何操作部多介紹,看下面提供的說明,比較簡單:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Usage:
jmap [option] <pid>
(to connect to running process)
jmap [option] <executable <core>
(to connect to a core file)
jmap [option] [[email protected]]<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 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.
-h | -help to print this help message
-J<flag> to pass <flag> directly to the runtime system
jhat 虛擬機器堆快照分析工具
我們可以使用 jhat 來分析 jmap 產生的 dump 檔案
1
2
3
4
5
6
7
8
9
10
11
12
13
>jhat tmp.dump
Reading from tmp.dump...
Dump file created Sat May 09 17:10:52 CST 2015
Snapshot read, resolving...
Resolving 0 objects...
WARNING: hprof file does not include java.lang.Class!
WARNING: hprof file does not include java.lang.String!
WARNING: hprof file does not include java.lang.ClassLoader!
Chasing references, expect 0 dots
Eliminating duplicate references
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.
預設會開 7000 連接埠進行 web 訪問。一般不使用這個命令來分析,會使用專業的工具來分析 dump 檔案,如 eclipse memory analyzer 等。
jstack 分析 java 堆棧
jstack 用來產生當前時刻線程快照。
使用方式如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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] [[email protected]]<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
【參考資料】
1.深入理解 JAVA 虛擬機器
在 java 的 bin 目錄下,jdk 提供了很多使用的工具,下面學習一些監控和故障處理的工具。