Wednesday, 11 May 2016

[Java] How to dump and analyze jvm heap memory using jmap, jhat (jmap, jhat을 이용한 JVM 힙메모리 덤프 및 분석)

JDK(Java Development Kit) has various java tools like javac, jps, jmap, jhat.

Sometimes, you want to analyze heap memory of running java process. For example, in case such as OutOfMemoryError.

If want to do that, you can use jmap and jhat java tools.

1. jmap

jmap prints shared object memory maps or heap memory details of a given process or core file or a remote debug server. If the given process is running on a 64-bit VM, you may need to specify the -J-d64 option.

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
    -permstat            to print permanent generation 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. jhat

The jhat command parses a java heap dump file and launches a web server. jhat enables you to browse heap dumps using your favorite web browser. jhat supports pre-designed queries (such as 'show all instances of a known class "Foo"') as well as OQL (Object Query Language) - a SQL-like query language to query heap dumps.


C:\Users\Administrator>jhat
ERROR: No arguments supplied
Usage:  jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>

        -J<flag>          Pass <flag> directly to the runtime system. For
                          example, -J-mx512m to use a maximum heap size of 512MB
        -stack false:     Turn off tracking object allocation call stack.
        -refs false:      Turn off tracking of references to objects
        -port <port>:     Set the port for the HTTP server.  Defaults to 7000
        -exclude <file>:  Specify a file that lists data members that should
                          be excluded from the reachableFrom query.
        -baseline <file>: Specify a baseline object dump.  Objects in
                          both heap dumps with the same ID and same class will
                          be marked as not being "new".
        -debug <int>:     Set debug level.
                            0:  No debug output
                            1:  Debug hprof file parsing
                            2:  Debug hprof file parsing, no server
        -version          Report version number
        -h|-help          Print this help and exit
        <file>            The file to read

For a dump file that contains multiple heap dumps,
you may specify which dump in the file
by appending "#<number>" to the file name, i.e. "foo.hprof#3".

All boolean options default to "true"

3. Example

3.1. Check PID

C:\Users\Administrator>jps -ml
22160 sun.tools.jps.Jps -ml
26688
18736 BTCBrokerBridge.jar
40704
30532
6408 org/netbeans/Main --branding visualvm --cachedir C:\Users\Administrator\AppData\Local\VisualVM\Cache/7u14 --openid 5804750059758362
4204 com.sun.jme.toolkit.bootstrap.Container
19508
27316
35124 D:\activemq\apache-activemq-5.11.4\bin\../bin/activemq.jar start
40400

3.2. Dump Heap memory map


C:\Users\Administrator>jmap -dump:format=b,file=D:/activemq.dump 35124
Dumping heap to D:\activemq.dump ...
Heap dump file created

3.3. Analyze Heap memory


C:\Users\Administrator>jhat D:/activemq.dump
Reading from D:/activemq.dump...
Dump file created Wed May 11 13:57:33 KST 2016
Snapshot read, resolving...
Resolving 251686 objects...
Chasing references, expect 50 dots..................................................
Eliminating duplicate references..................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.




No comments:

Post a Comment