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
0x150
0x1505mo ago
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.
0x150
0x1505mo ago
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
0x150
0x1505mo ago
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
0x150
0x1505mo ago
Xx HeapDumpOnOom or smth like that
Submission from elspon
0x150
0x1505mo ago
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
0x150
0x1505mo ago
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
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Submission from h31l0w0r1d

Did you find this page helpful?