openjdk 1.8
1.命令预览
jmap(Memory Map for Java)命令用于生成堆转储快照(一般称为heapdump或dump文件)。
如果不使用jmap命令,要想获取Java堆转储快照也还有一些比较“暴力”的手段:譬如在第2章中用过的-XX:+HeapDumpOnOutOfMemoryError
参数,可以让虚拟机在内存溢出异常出现之后自动生成堆转储快照文件,通过-XX:+HeapDumpOnCtrlBreak
参数则可以使用[Ctrl]+[Break]键让虚拟机生成堆转储快照文件,又或者在Linux
系统下通过Kill-3
命令发送进程退出信号“恐吓”一下虚拟机,也能顺利拿到堆转储快照。
jmap的作用并不仅仅是为了获取堆转储快照,它还可以查询finalize执行队列、Java堆和方法区的详细信息,如空间使用率、当前用的是哪种收集器等。
和jinfo命令一样,jmap有部分功能在Windows平台下是受限的,除了生成堆转储快照的-dump选项和用于查看每个类的实例、空间占用统计的-histo选项在所有操作系统中都可以使用之外,其余选项都只能在Linux/Solaris中使用。
./jmap -help 查看命令语法:
/zxjdk-8u212-linux-x64/bin # ./jmap -help
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
2.jmap -heap [pid]
查看堆信息
/zxjdk-8u212-linux-x64/bin # ./jmap -heap 466
Attaching to process ID 466, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.212-b04
using thread-local object allocation.
Parallel GC with 4 thread(s)
Heap Configuration:
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
MaxHeapSize = 1799356416 (1716.0MB)
NewSize = 149946368 (143.0MB)
MaxNewSize = 599785472 (572.0MB)
OldSize = 300941312 (287.0MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 21807104 (20.796875MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 17592186044415 MB
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
PS Young Generation
Eden Space:
capacity = 516947968 (493.0MB)
used = 198541616 (189.3440399169922MB)
free = 318406352 (303.6559600830078MB)
38.40649896896393% used
From Space:
capacity = 38273024 (36.5MB)
used = 37448744 (35.713905334472656MB)
free = 824280 (0.7860946655273438MB)
97.84631598485659% used
To Space:
capacity = 42467328 (40.5MB)
used = 0 (0.0MB)
free = 42467328 (40.5MB)
0.0% used
PS Old Generation
capacity = 344457216 (328.5MB)
used = 45055032 (42.96782684326172MB)
free = 299402184 (285.5321731567383MB)
13.080008171464755% used
24269 interned Strings occupying 2341528 bytes.
3 jmap -histo [pid]
打印实例对象
-histo[:live] ,其中 live可选,仅打印活着对象,会触发gc
/zxjdk-8u212-linux-x64/bin # ./jmap -histo 466
num #instances #bytes class name
----------------------------------------------
1: 1304342 81119512 [C
2: 186490 72774112 [I
3: 479076 72164712 [B
4: 890482 21371568 java.lang.String
5: 357231 18005072 [Ljava.lang.Object;
6: 504966 12119184 org.apache.kafka.common.internals.PartitionStates$PartitionState
7: 254837 8154784 java.util.HashMap$Node
8: 149497 7175856 java.util.HashMap
4 jmap -dump
jmap -dump:file=2019-1112-2024.dump pid
文件后缀jump或hprof好像没区别,MemoryAnalyzer均能识别
/zxjdk-8u212-linux-x64/bin # ls
2019-1112-2024.dump
多个条件需要用逗号拼接,如只打印存活对象:
jmap -dump:live,file=2019-1112-2024.dump pid
也可以设置内存溢出自动导出dump文件(内存很大的时候,可能会导不出来)
1. -XX:+HeapDumpOnOutOfMemoryError
2. -XX:HeapDumpPath=./ (路径)