天天看點

學習筆記——JVM性能調優之 jmap

jmap(JVM Memory Map)指令可生成head dump檔案,還可查詢finalize執行隊列、Java堆和永久代的詳細資訊。

通過配置啟動參數:-XX:+HeapDumpOnOutOfMemoryError參數可以讓JVM出現OOM時自動生成dump檔案。

jmap

官方文檔連結位址:https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jmap.html
學習筆記——JVM性能調優之 jmap

指令格式:

jstat [ option ] pid

jstat [ option ] executable core

jstat [ option ] [pid] [ server-id@ ] remote-hostname-or-IP

option :指令參數。
pid : 要為其列印記憶體映射的程序ID。該程序必須是java程序,可通過jps命 令檢視。
     (process id for which the memory map is to be printed. The process must be a Java process. To get a list of Java processes running on a machine, jps may be used.)
executable:生成核心dump的Java可執行檔案。
     (Java executable from which the core dump was produced.)
core:需要列印配置資訊的核心檔案。
     (core file for which the memory map is to be printed.)
server-id:可選的唯一id,如果相同的遠端主機上運作了多台調試伺服器,用此選項參數辨別伺服器。
     (optional unique id, if multiple debug servers are running on the same remote host.)
remote-hostname-or-IP:遠端調試伺服器的IP位址或主機名。
     (remote debug server's (see jsadebugd) hostname or IP address.)      

option參數:

  •  < no option >  如果不使用任何選項,則将列印共享對象映射。

  對于目标VM中加載的每個共享庫,将會列印起始位址,映射的大小以及共享庫檔案的完整路徑。這類似于Solaris pmap實用程式。(When no option is used **jmap** prints shared object mappings. For each shared object loaded in the target VM, start address, the size of the mapping, and the full path of the shared object file are printed. This is similar to the Solaris **pmap** utility.)

學習筆記——JVM性能調優之 jmap

從前到後依次為:共享庫起始位址       映射大小      共享庫檔案的完整路徑

  • -heap  列印堆摘要資訊,列印使用的GC算法,堆配置資訊和堆記憶體使用資訊。(Prints a heap summary. GC algorithm used, heap configuration and generation wise heap usage are printed.)

學習筆記——JVM性能調優之 jmap
學習筆記——JVM性能調優之 jmap
學習筆記——JVM性能調優之 jmap
[admin@pod-in5kpa01k5 ~]$ jmap -heap 13074Attaching to process ID 13074, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.51-b03

using thread-local object allocation.
Parallel GC with 4 thread(s) #有兩種組合 其中新生代采用并行處理方式--Parallel Scavenge 收集器
                             # 老年代可選 -XX:+UseParallelGC -- PSMarkSweep(Serial Old) 串行
                             #           -XX:+UserParallelOldGC  --  Parallel Old     并行

Heap Configuration:   # 堆配置資訊(項目啟動前配置的JVM參數)
   MinHeapFreeRatio         = 0        # -XX:MinHeapFreeRatio 最小空閑比例(default 40)
   MaxHeapFreeRatio         = 100    # -XX:MaxHeapFreeRatio 最大空閑比例(default 70)
   MaxHeapSize              = 2147483648 (2048.0MB) # -XX:MaxHeapSize 堆空間最大大小
   NewSize                  = 715653120 (682.5MB)   # -XX:NewSize    新生代預設大小
   MaxNewSize               = 715653120 (682.5MB)   # -XX:MaxNewSize 新生代最大大小
   OldSize                  = 1431830528 (1365.5MB) # -XX:OldSize    老年代預設大小
   NewRatio                 = 2  # -XX:NewRatio  新生代:老年代(不包括永久代),2表示 1:2
   SurvivorRatio            = 8  # -XX:SurvivorRatio Survivor和Eden的比值,8辨別 1:1:8
   MetaspaceSize            = 268435456 (256.0MB) # 元空間預設大小
   CompressedClassSpaceSize = 1073741824 (1024.0MB) # 壓縮類 空間 預設大小
   MaxMetaspaceSize         = 17592186044415 MB   # -XX:MaxMetaspaceSize 元空間最大大小
   G1HeapRegionSize         = 0 (0.0MB)
   # 在使用G1垃圾收集器時,JVM會将堆空間分隔為若幹個Region,該參數用來指定每個Region空間的大小

Heap Usage:            #  堆記憶體實際使用情況
PS Young Generation # --- 新生代
Eden Space:            # Eden空間
   capacity = 537395200 (512.5MB)                # Eden區總容量
   used     = 133469040 (127.28599548339844MB)    # Eden區已使用   free     = 403926160 (385.21400451660156MB)    # Eden區剩餘容量   24.83629180163872% used        # 使用 百分比
From Space:            # From空間
   capacity = 89128960 (85.0MB)                    # From區總容量
   used     = 40510936 (38.634239196777344MB)    # From區已使用   free     = 48618024 (46.365760803222656MB)    # From區剩餘容量   45.4520461138557% used        # 使用 百分比
To Space:            # To空間
   capacity = 89128960 (85.0MB)        # To區總容量
   used     = 0 (0.0MB)                # To區已使用   free     = 89128960 (85.0MB)        # To區剩餘容量   0.0% used                    # 使用百分比
PS Old Generation    # --- 老年代
   capacity = 1431830528 (1365.5MB)    # 老年代總空間
   used     = 180256 (0.171905517578125MB)        # 老年代已使用   free     = 1431650272 (1365.3280944824219MB)    # 老年代剩餘容量   0.01258919938323874% used    # 使用 百分比29573 interned Strings occupying 3039224 bytes.      

View Code

  • -histo[:live]  顯示堆的統計資訊,包括對象數量,執行個體,記憶體占用,類名。

  列印虛拟機内部的類名稱将會帶有一個 * 字首。(Prints a histogram of the heap. For each Java class, number of objects, memory size in bytes, and fully qualified class names are printed. VM internal class names are printed with '*' prefix.)

    如果帶上:live,隻列印存活的對象。(If the live suboption is specified, then only active objects are counted.)

學習筆記——JVM性能調優之 jmap

B - byte、C - char、D - double、F - float、I - int、J - long、Z - boolean

[ - 表示數組(如[C 表示 char[])、[L + 類名 表示 其他對象

因為在 dump:live 前會進行  full gc,如果帶上 live 則隻統計活對象,是以不加 live 的堆大小要大于加 live堆的大小

jmap -histo:live > fileName 可将列印的資訊輸入到檔案中,友善檢視和比較

  • -clstats  列印堆記憶體永久代類加載器資訊。

  對于每個類加載器,列印其名稱、活躍度、位址、父類加載器 以及它所加載的類的數量和大小(Prints class loader wise statistics of Java heap. For each class loader, its name, how active it is, address, parent class loader, and the number and size of classes it has loaded are printed.)

學習筆記——JVM性能調優之 jmap
  • -finalizerinfo  列印等待回收的對象資訊。(Prints information about objects that are awaiting finalization.)

學習筆記——JVM性能調優之 jmap

Number of objects pending for finalization: 0 (等待回收的對象為0個)

  • -dump[:live,] format=b,file=filename  使用 hprof 二進制形式,輸出 jvm 的 heap 内容到檔案。

  live子選項是可選的。如果指定了live子選項,堆中隻有活動的對象會被轉儲。可以通過jhat打開該檔案,通過http去通路。(Dumps the Java heap in hprof binary format to filename. The live suboption is optional, but when specified, only the active objects in the heap are dumped. To browse the heap dump, you can use the jhat(1) command to read the generated file.)

學習筆記——JVM性能調優之 jmap
這個指令執行,JVM會将整個heap的資訊dump寫入到一個檔案,heap如果比較大的話,就會導緻這個過程比較耗時,
并且執行的過程中為了保證dump的資訊是可靠的,是以會暫停應用, 線上系統慎用。      

  jhat 二進制轉儲檔案名,通路IP + 端口号

學習筆記——JVM性能調優之 jmap
  • -F  當端口号不響應時,可通過 -F 強制執行。

  此模式不支援:live (Force. Use this option with the jmap -dump or jmap -histo option when the pid does not respond. The live suboption is not supported in this mode.)

學習筆記——JVM性能調優之 jmap