jmap (Memory Map for Java) 命令用于生成堆转储快照(一般称为heapdump 或 dump文件)。要想获取Java 堆转储快照不使用jmap命令还有一些比较“暴力”的手段,譬如使用-XX:+HeapDumpOnOutOfMemoryError参数,可以让虚拟机在OOM异常出现之后自动生成dump文件,也可以通过 -XX:+HeapDumpOnCtrlBreak 参数,可以让虚拟机收到 [Ctrl]+[Break]键生成dump文件,又或者在Linux 系统下通过kill -3 命令发送进程退出信息“恐吓”一下虚拟机,也能拿到dump文件。
jmap 的作用并不仅仅是为了获取dump文件,还可以查询finalize执行队列,Java堆和永久代的详细信息,如空间使用率、当前用的是那种收集器等。
jmap 除了生成dump文件的 -dump 选项和用于查询每个类的实现、空间占用统计的 -histo 选项所有操作系统都提供之外,其余选项都只能在Linux/Unix下使用。
jmap 命令格式:
jmap [ option ] 虚拟机进程ID
option 选项的合法值与具体含义如下:
webadmin@xuexiyuan:~$ jmap -h 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
示例一:使用jmap 生成一个正在运行的Tomcat 的dump 快照文件的,命令及运行截图如下:
ps -ef | grep tomcat #查询tomcat进程id jmap -dump:format=b,file=tomcat_blog.dump 17296 #在当前目录生成正在运行的Tomcat快照文件 jmap -dump:live,format=b,file=tomcat_blog_live.dump 17296
示例二:使用jmap 显示Java 堆中对象统计信息,如类、实例数量和合计容量
jmap -histo 17296 | more #使用more分页查询信息 jmap -histo 17296 | more | grep cn.xuexiyuan #more分页并使用grep过滤不想看到的数据 jmap -histo:live 17296 | grep cn.xuexiyuan | more
官网教程:https://docs.oracle.com/javase/8/docs/technotes/tools/windows/jmap.html