天天看點

JVM 問題排查和性能優化常用的 JDK 工具

JDK 提供了一系列用于監控、診斷 Java 程序的工具,它們在 JDK 安裝目錄的 bin 目錄下,有 jps、jcmd、jstack、jinfo、jmap 等。其中jmc、jconsole、jvisualvm 是 GUI 工具,其他大部分都是指令行工具。

cd $JAVA_HOME/bin
ls
           
JVM 問題排查和性能優化常用的 JDK 工具

本篇隻是個入門介紹,不涉及深入分析。每一個工具都有它專門的作用,掌握使用方法隻是很簡單的入門階段,更重要的是根據工具得到的資訊去分析系統存在的問題以及性能瓶頸,每一個工具的使用和分析都可以單獨成文。

jps

如果你用過 Linux,那肯定熟悉 ps 指令,用來檢視程序清單的。jps 就好比是 ps 指令的子集,它查詢的是目前使用者下已經啟動的 Java 程序。這是進行線上問題排查的大門鑰匙,有了它才能下手後面的動作。

下面是 jps 的幫助文檔

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

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

一般的用法是

jps -l

,前面一列顯示 pid,後面一列顯示程序名稱。

JVM 問題排查和性能優化常用的 JDK 工具

還可以用下列參數檢視更具體的 Java 程序資訊,用法為

jps -lv

JVM 問題排查和性能優化常用的 JDK 工具

jstack

檢視 Java 程序内目前時刻的線程快照,也就是每條線程正在執行的方法棧情況,用于定位線程停頓、死鎖等長時間等待的問題。

以下是 jstack 的幫助文檔。

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 -pid 或者 jstack -l pid

,列印線程狀态、棧使用情況。

JVM 問題排查和性能優化常用的 JDK 工具

如果是線上檢視不友善的話,可以用指令

jstack -l pid > stack.log

,輸出到檔案中下載下傳到本地檢視。

jstack -m pid

,列印 Java 和 Native 棧資訊

JVM 問題排查和性能優化常用的 JDK 工具

如果 -l 和 -m 都不起作用的時候,可以使用

java -F pid

強制 dump。

jinfo

它的主要作用是檢視 JVM 配置參數,還可以動态設定部分參數值。jinfo 使用時需要 attach 到目标 JVM 上。關于 attach jvm 可以點選檢視 「Java 調試工具、熱部署、JVM 監控工具都用到了它」

使用

jinfo -h

檢視幫助文檔

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

檢視 JVM 參數,其中 Non-default VM flags 是虛拟機預設設定的參數,Command line 是使用者指定的參數,比如指令行啟動 jar 包的時候加上的參數。

jinfo -flag 參數名 pid

可以檢視指定參數的值,比如檢視堆的最大值(-XX:MaxHeapSize 也就是 -Xmx ):

jinfo -flag MaxHeapSize 92041

-XX:MaxHeapSize=20971520
           

jinfo -sysprops pid

檢視系統參數

jinfo pid

檢視 jvm 參數和系統參數

以上資訊,如果我們用過 visualVM 等監控工具,一定非常熟悉。另外,我之前做過一個 web 版的 JVM 監控器,也實作了這個功能。

JVM 問題排查和性能優化常用的 JDK 工具

另外,還可以修改部分參數值。

jinfo -flag [+|-] pid

jinfo -flag = pid

可以修改部分 JVM 參數。

前者可以修改布爾值參數,比如開啟簡單 GC 日志

jinfo -flag +PrintGC 92041
           

後者是設定非布爾值參數的,比如設定 HeapDumpPath

jinfo -flag HeapDumpPath=/users/fengzheng/jvmlog
           

哪些參數是允許動态修改的呢,用下面這個指令可以檢視

#Linux 和 Mac 
java -XX:+PrintFlagsInitial | grep manageable

#windows
java -XX:+PrintFlagsInitial | findstr manageable
           
JVM 問題排查和性能優化常用的 JDK 工具

jmap

jmap 檢視給定程序、核心檔案、遠端調試伺服器的共享對象記憶體映射和堆記憶體細節的工具,可檢視堆使用情況、堆内對象直方圖、加載類、生成堆快照等。

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                to print java heap summary
    -histo[:live]        to print histogram of java object heap; if the "live"
                         suboption is specified, only count live objects
    -clstats             to print class loader 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
           

jmap -heap pid

列印 JVM 堆概要資訊,包括堆配置、新生代、老生代資訊

JVM 問題排查和性能優化常用的 JDK 工具

jmap -histo pid

列印類的直方圖,也就是各個類執行個體的個數和空間占用情況。

如果加 :live,

jamp -histo:live pid

則隻列印活動類的資訊。這個指令會出發 GC 動作,會導緻 JVM 停頓,是以線上上環境要慎用。

jmap -dump

dump 目前 JVM 堆,一般用法如下:

#dump 所有對象在堆中的分布情況
jmap -dump:format=b,file=/Users/fengzheng/jvmlog/jamp_dump.hprof 95463

#加:live 參數 dump 存活對象在隊中的分布情況
jmap -dump:live,format=b,file=/Users/fengzheng/jvmlog/jamp_dump.hprof 95463
           

之後再用堆分析工具,比如 visualVM、JProfile、MAT 等進行分析。和我們設定

-XX:+HeapDumpOnOutOfMemoryError 參數後,在發生 OOM 的時候 dump 的堆資訊是一樣的。

注意,dump 的過程會比較慢,在這個過程中會發生 JVM 停頓,而且在使用 :live 參數後,會觸發 GC 操作。

jmap -clstats pid

Java 類加載器(ClassLoader)資訊,包括加載器名稱、已加載類個數、占用空間、父加載器、是否存活、類型資訊。

JVM 問題排查和性能優化常用的 JDK 工具

jmap -finalizerinfo pid

檢視等待被回收的對象。

JVM 問題排查和性能優化常用的 JDK 工具

jstat

jstat 主要用來通過垃圾回收相關資訊來判斷 JVM 性能問題,也可以檢視類加載、編譯的情況,主要的用法是通過持續的固定時間間隔的輸出來觀察。比如每 3 秒列印一次 GC 回收次數,連續列印 10 次,通過動态的變化來觀察 GC 是否過于密集。

下面是 jstat 的幫助手冊。

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

Definitions:
  <option>      An option reported by the -options option
  <vmid>        Virtual Machine Identifier. A vmid takes the following form:
                     <lvmid>[@<hostname>[:<port>]]
                Where <lvmid> is the local vm identifier for the target
                Java virtual machine, typically a process id; <hostname> is
                the name of the host running the target Java virtual machine;
                and <port> is the port number for the rmiregistry on the
                target host. See the jvmstat documentation for a more complete
                description of the Virtual Machine Identifier.
  <lines>       Number of samples between header lines.
  <interval>    Sampling interval. The following forms are allowed:
                    <n>["ms"|"s"]
                Where <n> is an integer and the suffix specifies the units as 
                milliseconds("ms") or seconds("s"). The default units are "ms".
  <count>       Number of samples to take before terminating.
  -J<flag>      Pass <flag> directly to the runtime system.
           

通過

jstat -options

可以看到 jstat 支援檢視哪些資訊。

$ jstat -options
-class  #類加載情況 加載個數和空間使用
-compiler #即時編譯器資訊
-gc  # GC情況 包括 young gc、full gc 次數、時間等
-gccapacity #年輕代、老年代的使用情況
-gccause #GC 統計資訊和回收原因
-gcmetacapacity #顯示有關metaspace大小的統計資訊
-gcnew #新生代 GC 統計
-gcnewcapacity #新生代記憶體統計
-gcold #老年代 GC 統計
-gcoldcapacity #老年代記憶體使用情況
-gcutil #GC 彙總資訊
-printcompilation #編譯方法統計
           

上述這些大多數可以對應到 visualVM 的這一部分顯示

JVM 問題排查和性能優化常用的 JDK 工具

示例用法,如下是列印 5301 程序下的垃圾回收情況,-h 3 表示每 3 行輸出一次标題資訊,3s 5 表示每 3s 輸出一次,一共輸出 5 次

jstat -gcutil -h 3 5301 3s 5
           

最後輸出的内容如下:

jstat -gcutil -h 3 5301 3s 5
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
 99.92   0.00  11.90  35.29  94.96  94.08     34   12.675     3    1.946   14.621
 99.92   0.00  11.90  35.29  94.96  94.08     34   12.675     3    1.946   14.621
 99.92   0.00  11.90  35.29  94.96  94.08     34   12.675     3    1.946   14.621
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
 99.92   0.00  11.94  35.29  94.96  94.08     34   12.675     3    1.946   14.621
 99.92   0.00  11.94  35.29  94.96  94.08     34   12.675     3    1.946   14.621
           

jcmd

jcmd 會将指令發送給 JVM。這些指令包括用于控制 Java Flight Recording(飛行記錄)、診斷指令等。 必須運作在 JVM 本地,不能遠端使用,并且必須用 JVM 啟動使用者執行。

通過 jps 指令找到一個 JVM 程序,然後使用下面的代碼可以看到 jcmd 支援的指令

#程序 5173 
jcmd 5173 help 

5173:
The following commands are available:
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.native_memory
VM.check_commercial_features
VM.unlock_commercial_features
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
GC.rotate_log
Thread.print
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.run_finalization
GC.run
VM.uptime
VM.flags
VM.system_properties
VM.command_line
VM.version
help
           

基本包含了問題排查的常用指令,并且和上面介紹的幾個工具有部分重合。

通過指令

jcmd 5173 help GC.heap_dump

可以查詢到 GC.heap_dump 指令的使用方法,其他指令都可以通過這個方法找到使用說明

jcmd 5173 help GC.heap_dump
5173:
GC.heap_dump
Generate a HPROF format dump of the Java heap.

Impact: High: Depends on Java heap size and content. Request a full GC unless the '-all' option is specified.

Permission: java.lang.management.ManagementPermission(monitor)

Syntax : GC.heap_dump [options] <filename>

Arguments:
	filename :  Name of the dump file (STRING, no default value)

Options: (options must be specified using the <key> or <key>=<value> syntax)
	-all : [optional] Dump all objects, including unreachable objects (BOOLEAN, false)
           

然後通過如下代碼就可以 dump 堆資訊下來了,和 jmap -dump 的作用一樣

jcmd 5173 GC.heap_dump /Users/fengzheng/jvmlog/jcmd_heap_dump.hprof
           

抛磚引玉就到此了,之後會對 jinfo、jmap、jstack、jstat、jcmd 做詳細說明,記得關注啊。

相關閱讀:

JVM 你不可不知的參數

無意中就做了個 web 版 JVM 監控端

JConsole、VisualVM 依賴的 JMX 技術

Java 調試工具、熱部署、JVM 監控工具都用到了它

我們說的 JVM 記憶體是什麼

歡迎關注,不定期更新本系列和其他文章

古時的風筝

,進入公衆号可以加入交流群

JVM 問題排查和性能優化常用的 JDK 工具

人生沒有回頭路,珍惜當下。