1. jmap
1.1 概述
JVM Memory Map命令用於生成heap dump文件,如果不使用這個命令,還可以使用-XX:+HeapDumpOnOutOfMemoryError參數來讓虛擬機出現OOM的時候自動生成dump文件。
jmap不僅能生成dump文件,還可以查詢finalize執行隊列、Java堆和老年代的詳細信息,如當前使用率、當前使用的是哪種收集器等。
> 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
1.2 參數
option:選項參數,不可同時使用多個選項參數。
pid:Java進程id。
executable:產生核心dump的Java可執行文件。
core:需要打印配置信息的核心文件。
remote-hostname-or-ip:遠程調試的主機名或ip。
server-id:可選的唯一id,如果相同的遠程主機上運行了多台調試服務器,用此選項參數標示服務器。
1.3 options參數
heap:顯示Java堆詳細信息;
histo:線下堆中對象的統計信息;
clstats:Java堆中內存的類加載器的統計信息;
finalizerinfo:顯示在F-Queue隊列等待Finlizer線程執行finalizer方法的對象;
dump:生成堆轉儲快照;
F:當-dump沒有響應時,強制生成dump快照;
2. 用法
所有測試基於如下JDK版本:
> java -version java version "1.8.0_121" Java(TM) SE Runtime Environment (build 1.8.0_121-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
2.1 jmap -dump:live,format=b,file=dump.hprof 129665
dump堆到文件,format指定輸出格式,live指明是活着的對象,file指定文件名。
> jmap -dump:live,format=b,file=dump.hprof 129665 Dumping heap to /opt/huawei/inputMethod/flight/flight/dump.hprof ... Heap dump file created
dump.hprof這個文件可以通過eclipse的打開:
2.2 jmap -heap 129665
打印heap的概要信息,GC使用的算法,heap的配置和使用情況,可以用此來判斷內存目前的使用情況以及垃圾回收情況。
> jmap -heap 129665 Attaching to process ID 129665, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.121-b13 using parallel threads in the new generation. using thread-local object allocation. Concurrent Mark-Sweep GC Heap Configuration: MinHeapFreeRatio = 40 MaxHeapFreeRatio = 70 MaxHeapSize = 2147483648 (2048.0MB) NewSize = 805306368 (768.0MB) MaxNewSize = 805306368 (768.0MB) OldSize = 1342177280 (1280.0MB) NewRatio = 2 SurvivorRatio = 8 MetaspaceSize = 21807104 (20.796875MB) CompressedClassSpaceSize = 1073741824 (1024.0MB) MaxMetaspaceSize = 17592186044415 MB G1HeapRegionSize = 0 (0.0MB) Heap Usage: New Generation (Eden + 1 Survivor Space): capacity = 724828160 (691.25MB) used = 291033744 (277.55140686035156MB) free = 433794416 (413.69859313964844MB) 40.15210225827871% used Eden Space: capacity = 644349952 (614.5MB) used = 291033744 (277.55140686035156MB) free = 353316208 (336.94859313964844MB) 45.167031222189024% used From Space: capacity = 80478208 (76.75MB) used = 0 (0.0MB) free = 80478208 (76.75MB) 0.0% used To Space: capacity = 80478208 (76.75MB) used = 0 (0.0MB) free = 80478208 (76.75MB) 0.0% used concurrent mark-sweep generation: capacity = 1342177280 (1280.0MB) used = 4730600 (4.511451721191406MB) free = 1337446680 (1275.4885482788086MB) 0.3524571657180786% used 7119 interned Strings occupying 644232 bytes.
2.3 jmap -finalizerinfo 129665
打印等待回收的對象信息。
> jmap -finalizerinfo 129665 Attaching to process ID 129665, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.121-b13 Number of objects pending for finalization: 0
Number of objects pending for finalization:0 說明當前F-Queue隊列中並沒有等待Finalizer線程執行finalizer方法的對象。
2.4 jmap -histo:live 129665
打印堆的對象統計,包括對象數、內存大小等。jmap -histo:live這個命令執行,JVM會先觸發gc,然后再統計信息。
第一列:編號id
第二列:實例個數
第三列:所有實例大小
第四列:類名
> jmap -histo:live 129665 | grep com.netflix 658: 1 40 com.netflix.hystrix.strategy.HystrixPlugins 790: 1 24 com.netflix.hystrix.strategy.properties.HystrixDynamicPropertiesSystemProperties$4 934: 1 16 com.netflix.hystrix.strategy.properties.HystrixDynamicPropertiesSystemProperties
2.5 jmap -clstats 129665
打印Java類加載器的智能統計信息,對於每個類加載器而言,對於每個類加載器而言,它的名稱,活躍度,地址,父類加載器,它所加載的類的數量和大小都會被打印。此外,包含的字符串數量和大小也會被打印。
jmap -clstats 129665 Attaching to process ID 129665, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.121-b13 finding class loader instances ..done. computing per loader stat ..done. please wait.. computing liveness.liveness analysis may be inaccurate ... class_loader classes bytes parent_loader alive? type <bootstrap> 1574 2848839 null live <internal> 0x00000000b002b6a0 1224 2203269 0x00000000b002d248 live sun/misc/Launcher$AppClassLoader@0x000000010000f6a0 0x00000000b02f3a68 0 0 0x00000000b002b6a0 dead java/util/ResourceBundle$RBClassLoader@0x000000010007aad8 0x00000000b0003e58 1 1471 null dead sun/reflect/DelegatingClassLoader@0x0000000100009df8 0x00000000b002ce78 1 1471 null dead sun/reflect/DelegatingClassLoader@0x0000000100009df8 0x00000000b002d248 112 240746 null live sun/misc/Launcher$ExtClassLoader@0x000000010000fa48 total = 6 2912 5295796 N/A alive=3, dead=3 N/A
2.6 -F
強制模式。如果指定的pid沒有響應,請使用jmap -dump或jmap -histo選項。此模式下,不支持live子選項。
比如:
jmap -F -histo 129665 | grep com.netflix Iterating over heap. This may take a while... 733: 1 40 com.netflix.hystrix.strategy.HystrixPlugins 873: 1 24 com.netflix.hystrix.strategy.properties.HystrixDynamicPropertiesSystemProperties$4 1047: 1 16 com.netflix.hystrix.strategy.properties.HystrixDynamicPropertiesSystemProperties Heap traversal took 7.434 seconds.