liveBook · Manning

Heap dump

As explained in Troubleshooting Java, Second Edition

A heap dump is a diagnostic snapshot of the objects, values, and references present in a Java heap at a particular time.

A heap dump is a diagnostic snapshot of an application’s Java heap at a specific moment. It records objects stored in memory, their values, and relationships between references. This makes it useful when an application crashes or cannot be profiled, especially after an OutOfMemoryError. section 1.2.4 section 10.0

Use a heap dump to investigate memory allocation problems, memory leaks, excessive object retention, and inefficient data structures. An OutOfMemoryError identifies the failure, but not necessarily the code that retained the objects; the dump provides evidence about what remained in memory and why. section 5.2.3 section 10.4

Obtain and inspect a heap dump through a practical investigation:

  • Configure the Java process to create a dump automatically when an OutOfMemoryError occurs. section 10.1
  • Capture a running local process with VisualVM by selecting the process and clicking Heap Dump in the Monitor tab. section 10.1.2
  • Generate a dump with command-line tools such as jcmd or jmap, or obtain one programmatically through framework capabilities. section 10.1
  • Open the dump in VisualVM, verify its file and system summary, then inspect the Objects view. section 10.2
  • Sort object types by instance count or retained size, find the first type from the application codebase, and trace its references back to fields or collections. section 10.2

A growing Product list

A Java application repeatedly adds Product objects containing generated String values to a static ArrayList. The list retains the objects, so memory fills until an OutOfMemoryError occurs. A heap dump reveals that Product and String instances occupy much of the heap, while reference inspection shows the ArrayList holding more than one million objects through a static field in Main.

Figure 10.3 Click the Heap Dump button in VisualVM’s Monitor tab to get a heap dump for the selected process. VisualVM opens the dump as a tab, and you can further investigate it or save it anywhere you want.

VisualVM’s Heap Dump control provides a concrete starting point for capturing and opening a dump from a selected running process. section 10.1.2

How Troubleshooting Java, Second Edition covers it

How deep it goes
Explains capture methods, VisualVM inspection, object sorting, and reference tracing, while deferring advanced querying to OQL.
Examples
Uses Product and String instances retained by a static ArrayList, plus VisualVM object and reference views.
What you can do after
Diagnose memory leaks and retained objects with VisualVM, command-line dumps, object views, reference paths, and OQL.
How it connects
Connects heap dumps with OutOfMemoryError, memory leaks, garbage collection, VisualVM, thread dumps, and OQL.

Also explained in

Compare how 2 books explain heap dump →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Troubleshooting Java, Second Edition. The section links point to the parts of the book it draws on.