Week 86 — What is a heap dump and how can it be obtained from a Java application?
Question of the Week #86
What is a heap dump and how can it be obtained from a Java application?
6 Replies
A heap dump contains a snapshot of the heap with the objects that are currently in memory of the Java application. This is useful for debugging memory leaks or other issues where the application crashes due to going out of memory.
An easy way to create a heap dump is to automatically let the JVM create one when the application goes out of memory. This can be configured using the
-XX:+HeapDumpOnOutOfMemoryError
JVM option. A Java application started with -XX:+HeapDumpOnOutOfMemoryError
will create a heap dump when it crashes due to running out of memory while a Java application started with -XX:-HeapDumpOnOutOfMemoryError
doesn't.Apart from that, it's also possible to create a heap dump of a Java program while it is running. This can be done using the
jcmd
program. To do this, first obtain the process ID of the running Java application and then run jcmd PID GC.heap_dump filename=/PATH/TO/HEAPDUMP.hprof
where PID
is the process ID and /PATH/TO/HEAPDUMP.hprof
is the path where the heap dump should be created at.📖 Sample answer from dan1st
Heap dumps provide information on all objects in memory at a point in time, usually an out of memory error. It can be obtained on an OOM crash by specifying a command line argument
Xx HeapDumpOnOom or smth like that
Submission from elspon
The heap dump is a picture of all the running objects at a certain point in time in a JVM and a Java VisualVM can be used to obtain one
Submission from s1thecause
heap dump is basically a snapshot of heap memory at particular instant of time
we can use tools like visual vm, and monitoring services by using concept of observability. In springboot the following dependency help us with that
Submission from h31l0w0r1d