欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JVM探秘:jmap生成内存堆转储快照

程序员文章站 2024-02-07 11:15:28
本系列笔记主要基于《深入理解Java虚拟机:JVM高级特性与最佳实践 第2版》,是这本书的读书笔记。 jmap 命令用来生成内存堆转储快照,一般称为heapdump或dump文件。 除了使用 jmap 命令,还以通过一些JVM参数让虚拟机在内存溢出时自动dump出快照文件。 | 参数 | 说明 | ......

本系列笔记主要基于《深入理解java虚拟机:jvm高级特性与最佳实践 第2版》,是这本书的读书笔记。

jmap 命令用来生成内存堆转储快照,一般称为heapdump或dump文件。
除了使用 jmap 命令,还以通过一些jvm参数让虚拟机在内存溢出时自动dump出快照文件。

参数 说明
-xx:+heapdumponoutofmemoryerror 内存溢出时自动导出内存快照
-xx:heapdumppath=e:/dumps/ 导出内存快照时保存的路径

所有的jdk工具都可以在oracle官网的 java tools reference 文档中找到使用说明,这是主要参考,包括命令格式、参数内容、输出信息等等。

jmap 命令格式:

jmap [ options ] pid
jmap [ options ] executable core
jmap [ options ] [ pid ] server-id@ ] remote-hostname-or-ip

jmap 帮助信息:

JVM探秘:jmap生成内存堆转储快照

jmap -dump

jmap 命令使用-dump参数生成内存快照,-dump参数格式如下:

-dump:[live,] format=b, file=filename

举例如下:

jmap -dump:format=b,file=/dumps/jmap.hprof 21060

输出如下:

JVM探秘:jmap生成内存堆转储快照

可见,在指定目录dumps下面生成了堆快照文件。

jmap -heap

jmap 命令使用-heap参数查看堆内存的配置信息,以及堆中各个区域的使用情况,如新生代的eden和survivor区,还有老年代。

-heap参数举例如下:

jmap -heap 21060

输出如下:

JVM探秘:jmap生成内存堆转储快照

其他参数

jmap 命令还有很多其他参数,可以参考 java tools reference 文档,列举如下:

<no option>
    when no option is used, the jmap command prints shared object mappings. for each shared object loaded in the target jvm, the start address, size of the mapping, and the full path of the shared object file are printed. this behavior is similar to the oracle solaris pmap utility.
    
-dump:[live,] format=b, file=filename
    dumps the java heap in hprof binary format to filename. the live suboption is optional, but when specified, only the active objects in the heap are dumped. to browse the heap dump, you can use the jhat(1) command to read the generated file.
    
-finalizerinfo
    prints information about objects that are awaiting finalization.
    
-heap
    prints a heap summary of the garbage collection used, the head configuration, and generation-wise heap usage. in addition, the number and size of interned strings are printed.

-histo[:live]
    prints a histogram of the heap. for each java class, the number of objects, memory size in bytes, and the fully qualified class names are printed. the jvm internal class names are printed with an asterisk (*) prefix. if the live suboption is specified, then only active objects are counted.

-clstats
    prints class loader wise statistics of java heap. for each class loader, its name, how active it is, address, parent class loader, and the number and size of classes it has loaded are printed.

-f
    force. use this option with the jmap -dump or jmap -histo option when the pid does not respond. the live suboption is not supported in this mode.

-h
    prints a help message.
    
-help
    prints a help message.
    
-jflag
    passes flag to the java virtual machine where the jmap command is running.