天天看點

[JAVA]JVM 性能調優監控工具jps、jstack、jmap、jhat、jstat、hprof 使用詳解

前言

現實企業級Java應用開發、維護中,有時候我們會碰到下面這些問題:

  • OutOfMemoryError,記憶體不足
  • 記憶體溢出
  • 線程死鎖
  • 鎖争用(Lock Contention)
  • Java程序消耗CPU或者Memory過高
  • ......

這些問題在日常的運維維護中可能被很多人忽視(大部分人遇到上面的問題隻是重新開機伺服器或者調大記憶體,而不是深究問題根源),但是能夠了解并解決這些問題是Java應用運維進階的必備要求

這裡介紹一下常見的jvm性能調優監控工具進行介紹,希望能起到抛磚引玉之用。

1、jps主要用來輸出jvm中的運作的程序狀态資訊。

jps是jdk提供的一個檢視目前java程序的小工具,可以看做是JavaVirtual Machine Process Status Tool的縮寫。非常簡單實用。

指令格式:jps [options ] [ hostid ]

 [options]選項 :

-q:僅輸出VM辨別符,不包括classname,jar name,arguments in main method 

-m:輸出main method的參數 

-l:輸出完全的包名,應用主類名,jar的完全路徑名 

-v:輸出jvm參數 

-V:輸出通過flag檔案傳遞到JVM中的參數(.hotspotrc檔案或-XX:Flags=所指定的檔案 

-Joption:傳遞參數到vm,例如:-J-Xms512m

基本使用:

[JAVA]JVM 性能調優監控工具jps、jstack、jmap、jhat、jstat、hprof 使用詳解
[JAVA]JVM 性能調優監控工具jps、jstack、jmap、jhat、jstat、hprof 使用詳解
[JAVA]JVM 性能調優監控工具jps、jstack、jmap、jhat、jstat、hprof 使用詳解

2、jstack主要用來檢視某個Java程序内的線程堆棧資訊

指令格式:

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. 會列印出額外的鎖資訊,在發生死鎖時可以用jstack -l pid來觀察鎖持有情況-m mixed mode,不僅會輸出Java堆棧資訊,還會輸出C/C++堆棧資訊(比如Native方法)

    -h or -help to print this help message

jstack可以定位到線程堆棧,根據堆棧資訊我們可以定位到具體代碼,是以它在JVM性能調優中使用得非常多。

實戰:

例:找出某個Java程序中最耗費CPU的Java線程并定位堆棧資訊,用到的指令有ps、top、printf、jstack、grep

首先,找到程序的PID

[JAVA]JVM 性能調優監控工具jps、jstack、jmap、jhat、jstat、hprof 使用詳解

 其次,找出該程序内最耗費CPU的線程,可以使用ps -Lfp pid或者ps -mp pid -o THREAD, tid, time或者top -Hp pid

[JAVA]JVM 性能調優監控工具jps、jstack、jmap、jhat、jstat、hprof 使用詳解

 TIME列就是各個Java線程耗費的CPU時間,CPU時間最長的是線程ID為12657的線程,用得到12657的十六進制值為3171,下面會用到。

然後,終于輪到jstack上場了,它用來輸出程序21711的堆棧資訊,然後根據線程ID的十六進制值grep

[JAVA]JVM 性能調優監控工具jps、jstack、jmap、jhat、jstat、hprof 使用詳解

 可以看到CPU消耗在http-nio-8002-exec-657這個類的condition。

後續可以正對這跟類的代碼進行分析

3、jmap導出堆記憶體,然後使用jhat來進行分析

指令格式:

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

    -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

如果運作在64位JVM上,可能需要指定-J-d64指令選項參數

實戰:

例:使用jmap -heap pid檢視程序堆記憶體使用情況,包括使用的GC算法、堆配置參數和各代中堆記憶體使用情況

[JAVA]JVM 性能調優監控工具jps、jstack、jmap、jhat、jstat、hprof 使用詳解

使用jmap -histo[:live] pid檢視堆記憶體中的對象數目、大小統計直方圖,如果帶上live則隻統計活對象

[JAVA]JVM 性能調優監控工具jps、jstack、jmap、jhat、jstat、hprof 使用詳解

class name是對象類型,說明如下:

B  byte
C  char
D  double
F  float
I  int
J  long
Z  boolean
[  數組,如[I表示int[]
[L+類名 其他對象
           

還有一個很常用的情況是:用jmap把程序記憶體使用情況dump到檔案中,再用jhat分析檢視

[JAVA]JVM 性能調優監控工具jps、jstack、jmap、jhat、jstat、hprof 使用詳解

 dump出來的檔案可以用MAT、VisualVM等工具檢視

[JAVA]JVM 性能調優監控工具jps、jstack、jmap、jhat、jstat、hprof 使用詳解

注意如果Dump檔案太大,可能需要加上-J-Xmx512m這種參數指定最大堆記憶體,即jhat -J-Xmx512m -port 9998 /tmp/dump.dat。然後就可以在浏覽器中輸入主機位址:9998檢視。

4、jstat看看各個區記憶體和GC的情況 

 指令格式:

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.

vmid是Java虛拟機ID,在Linux/Unix系統上一般就是程序ID。interval是采樣時間間隔。count是采樣數目。比如下面輸出的是GC資訊,采樣時間間隔為250ms,采樣數為4

[JAVA]JVM 性能調優監控工具jps、jstack、jmap、jhat、jstat、hprof 使用詳解

要明白上面各列的意義,先看JVM堆記憶體布局:

[JAVA]JVM 性能調優監控工具jps、jstack、jmap、jhat、jstat、hprof 使用詳解

可以看出:

堆記憶體 = 年輕代 + 年老代 + 永久代
年輕代 = Eden區 + 兩個Survivor區(From和To)
           

現在來解釋各列含義:

S0C、S1C、S0U、S1U:Survivor 0/1區容量(Capacity)和使用量(Used)
EC、EU:Eden區容量和使用量
OC、OU:年老代容量和使用量
PC、PU:永久代容量和使用量
YGC、YGT:年輕代GC次數和GC耗時
FGC、FGCT:Full GC次數和Full GC耗時
GCT:GC總耗時
           

 5、hprof能夠展現CPU使用率,統計堆記憶體使用情況

指令格式:

java -agentlib:hprof[=options] ToBeProfiledClass

java -Xrunprof[:options] ToBeProfiledClass

javac -J-agentlib:hprof[=options] ToBeProfiledClass

實戰:

例1:CPU Usage Sampling Profiling(cpu=samples)

例2:Heap Allocation Profiling(heap=sites)

例3:Heap Dump(heap=dump)

雖然在JVM啟動參數中加入-Xrunprof:heap=sites參數可以生成CPU/Heap Profile檔案,但對JVM性能影響非常大,不建議線上上伺服器環境使用

結束