命令jmap是一个多功能的命令。它可以生成 java 程序的 dump 文件, 也可以查看堆内对象示例的统计信息、查看 ClassLoader 的信息以及 finalizer 队列。
使用方法
[root@localhost ~]# 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
参数:
- option: 选项参数。
- pid: 需要打印配置信息的进程ID。
- executable: 产生核心dump的Java可执行文件。
- core: 需要打印配置信息的核心文件。
- server-id 可选的唯一id,如果相同的远程主机上运行了多台调试服务器,用此选项参数标识服务器。
- remote server IP or hostname 远程调试服务器的IP地址或主机名。
option
- no option: 查看进程的内存映像信息,类似 Solaris pmap 命令。
- heap: 显示Java堆详细信息
- histo[:live]: 显示堆中对象的统计信息
- clstats:打印类加载器信息
- finalizerinfo: 显示在F-Queue队列等待Finalizer线程执行finalizer方法的对象
- dump::生成堆转储快照
- F: 当-dump没有响应时,使用-dump或者-histo参数. 在这个模式下,live子参数无效.
- help:打印帮助信息
- J:指定传递给运行jmap的JVM的参数
查看堆信息
jmap(jmap -heap PID)
启动一个springboot项目,使用Jps查看进程,使用jmap -heap 进程ID查看堆信息。
[root@localhost ~]# jmap -heap 1985
Attaching to process ID 1985, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.252-b09
using thread-local object allocation.
Parallel GC with 2 thread(s)
Heap Configuration:
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
MaxHeapSize = 1065353216 (1016.0MB)
NewSize = 22020096 (21.0MB)
MaxNewSize = 354942976 (338.5MB)
OldSize = 45088768 (43.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 = 131072000 (125.0MB)
used = 8751104 (8.345703125MB)
free = 122320896 (116.654296875MB)
6.6765625% used
From Space:
capacity = 4194304 (4.0MB)
used = 4161584 (3.9687957763671875MB)
free = 32720 (0.0312042236328125MB)
99.21989440917969% used
To Space:
capacity = 5242880 (5.0MB)
used = 0 (0.0MB)
free = 5242880 (5.0MB)
0.0% used
PS Old Generation
capacity = 31981568 (30.5MB)
used = 6904608 (6.584747314453125MB)
free = 25076960 (23.915252685546875MB)
21.58933545722336% used
11293 interned Strings occupying 989816 bytes.
操作系统信息:
[root@localhost ~]# top
top - 03:10:41 up 39 min, 2 users, load average: 0.00, 0.01, 0.05
Tasks: 114 total, 2 running, 112 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.2 us, 0.0 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 4158308 total, 3171288 free, 449576 used, 537444 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 3471820 avail Mem
jmap -histo PID查看对象内存占用情况
jmap -histo PID 查看对内对象占用空间大小,有高到低排序
C:\Users\admin>jmap -histo:live 15368
num #instances #bytes class name (module)
-------------------------------------------------------
1: 18585 9338256 [B (java.base@11.0.5)
2: 1463 1413320 [I (java.base@11.0.5)
3: 4159 496272 java.lang.Class (java.base@11.0.5)
4: 4855 424856 [Ljava.lang.Object; (java.base@11.0.5)
5: 17549 421176 java.lang.String (java.base@11.0.5)
6: 10657 341024 java.util.concurrent.ConcurrentHashMap$Node (java.base@11.0.5)
7: 222 169200 [C (java.base@11.0.5)
8: 3833 122656 java.util.HashMap$Node (java.base@11.0.5)
9: 6607 105712 java.lang.Object (java.base@11.0.5)
其中关于I、B、C等的说明如下 Table 4.2.
BaseType Character Type Interpretation
B byte signed byte
C char Unicode character
D double double-precision floating-point value
F float single-precision floating-point value
I int integer
J long long integer
L<classname>; reference an instance of class de><classname>de>
S short signed short
Z boolean de>truede> or de>falsede>
[ reference one array dimension
jmap -permstat 查看永久代
打印一些持久代上的内存使用状态,有“活”的,有“死”的。