jmap
jmap(JVM Memory Map)命令可生成head dump檔案,還可查詢finalize執行佇列、Java堆和永久代的詳細資訊,
通過配置啟動引數:-XX:+HeapDumpOnOutOfMemoryError引數可以讓JVM出現OOM時自動生成dump檔案,
官方檔案鏈接地址:https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jmap.html

命令格式:
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.)

從前到后依次為:共享庫起始地址 映射大小 共享庫檔案的完整路徑
-
-heap 列印堆摘要資訊,列印使用的GC演算法,堆配置資訊和堆記憶體使用資訊,(Prints a heap summary. GC algorithm used, heap configuration and generation wise heap usage are printed.)

[admin@pod-in5kpa01k5 ~]$ jmap -heap 13074 Attaching 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.)

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.)

-
-finalizerinfo 列印等待回收的物件資訊,(Prints information about objects that are awaiting finalization.)

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會將整個heap的資訊dump寫入到一個檔案,heap如果比較大的話,就會導致這個程序比較耗時,
并且執行的程序中為了保證dump的資訊是可靠的,所以會暫停應用, 線上系統慎用,
jhat 二進制轉儲檔案名,訪問IP + 埠號

-
-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.)

可強制執行,但指定不正確的埠號時會報錯,只有明確埠號但未回應時才可使用,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/262778.html
標籤:Java
