jmap命令用于打印或dump出java运行程序中的共享对象内存映射或堆内存细节。
查看堆信息和垃圾回收算法
jmap -heap 25282
其中25282是进程号,使用jps -l命令查出我们想看到的进程的进程id号。
输出
Attaching to process ID 25282, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.352-b08
using thread-local object allocation.
Parallel GC with 2 thread(s)
Heap Configuration:
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
MaxHeapSize = 994050048 (948.0MB)
NewSize = 20971520 (20.0MB)
MaxNewSize = 331350016 (316.0MB)
OldSize = 41943040 (40.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 = 289406976 (276.0MB)
used = 35245960 (33.61316680908203MB)
free = 254161016 (242.38683319091797MB)
12.178683626478996% used
From Space:
capacity = 19922944 (19.0MB)
used = 8716400 (8.312606811523438MB)
free = 11206544 (10.687393188476562MB)
43.75056216591283% used
To Space:
capacity = 18874368 (18.0MB)
used = 0 (0.0MB)
free = 18874368 (18.0MB)
0.0% used
PS Old Generation
capacity = 92798976 (88.5MB)
used = 78333632 (74.70477294921875MB)
free = 14465344 (13.79522705078125MB)
84.412172823976% used
34475 interned Strings occupying 3383400 bytes.
Debugger attached successfully.
Server compiler detected.
JVM version is 25.352-b08
using thread-local object allocation.
Parallel GC with 2 thread(s)
Heap Configuration:
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
MaxHeapSize = 994050048 (948.0MB)
NewSize = 20971520 (20.0MB)
MaxNewSize = 331350016 (316.0MB)
OldSize = 41943040 (40.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 = 289406976 (276.0MB)
used = 35245960 (33.61316680908203MB)
free = 254161016 (242.38683319091797MB)
12.178683626478996% used
From Space:
capacity = 19922944 (19.0MB)
used = 8716400 (8.312606811523438MB)
free = 11206544 (10.687393188476562MB)
43.75056216591283% used
To Space:
capacity = 18874368 (18.0MB)
used = 0 (0.0MB)
free = 18874368 (18.0MB)
0.0% used
PS Old Generation
capacity = 92798976 (88.5MB)
used = 78333632 (74.70477294921875MB)
free = 14465344 (13.79522705078125MB)
84.412172823976% used
34475 interned Strings occupying 3383400 bytes.
在上面的例子中使用的垃圾回收器是 Parallel GC。
PS Young Generation和PS Old Generation分别显示新生代和老年代两个区域的内存使用情况。
导出当前Java进程的内存占用情况
jmap -dump:<dump-options>
dump-options参数
- live 只dump存活的对象,如果不加则会dump所有对象
- format=b 表示以二进制格式
- file=filepath 输出到某个文件中
例如
jmap -dump:live,format=b,file=/data/dump_heap25282.bin 25282
其中25282是进程id号,/data/dump_heap25282.bin是导出文件的路径。输出
Dumping heap to /data/dump_heap25282.bin ...
Heap dump file created
Heap dump file created
可以使用VisualVM工具分析导出来的bin文件。点击顶部菜单的文件->载入,选择hprof格式。