Monday, May 26, 2014

How to take JVM Heap Dump using jmap

We have different options for taking a heap dump of running JAVA process and then analyze those dump for any kind for memory leak.
Today, We are going to do the same using "jmap" in-built utility provided with JDK. Using this you will get an advantage that you don't need to wait untill our JVM got crashed by using passing "-XX:+HeapDumpOnOutOfMemoryError" parameter to JVM process.

We can capture current status using jmap as below:
1.) Find out the jmap location(only if your JAVA_HOME variable not defined)
      #locate jmap
2.) After finding jmap location, move to that directory and run jmap to get live thread dump.
      #./jmap -dump:live,format=b,file=[file location] [pid]


For more details and options run the command with help option as below :

# ./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
    -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

Common Error that you can get is "[pid]: well-known file is not secure".
The error comes, when you run jmap with user other than with which Java process is running. To overcome this switch to the user with which java process is running and make sure that user has sufficient permission on the directory where you are going create heap dump file, otherwise you may get permission denied errors.


# ./jmap -dump:live,format=b,file=/[Path]/test.hprof 27114
27114: well-known file is not secure

Note: You can list running Java processes using jps(another in-built tool provided with JDK) somewhat similar to "ps" command.
e.g.
$ ./jps | grep -vi jps

Hope this will help you :) !!

Integrate Jenkins with Azure Key Vault

Jenkins has been one of the most used CI/CD tools. For every tool which we are using in our daily life, it becomes really challenges when ...