Chapter 11: Finding memory-related issues in an app’s execution
This introduction explains the consequences of poor memory management and why heap dumps can help investigate crashes.
After this chapter you will be able to
- Memory allocation consequences. Explain how inefficient memory allocation can affect an app’s performance and continued execution.
- Heap dumps after crashes. Explain why analyzing a heap dump can help investigate a memory-related crash when profiling is unavailable.
- Sampling memory usage patterns. Describe how to sample memory usage and interpret the results to distinguish many objects from a few large objects.
- Targeted profiling expressions. Apply VisualVM memory profiling expressions to select a specific type, a package, or a package with its subpackages, and explain why the selection should be narrow.
- Profiling allocation details. Explain when to follow sampling with profiling and what allocation details profiling provides.
- Memory leak crash mechanism. Explain how a memory leak can lead to an application crash and how the JVM signals memory exhaustion.
- Heap dump investigation role. Describe what a heap dump captures and why it is useful when an application cannot be profiled.
- Automatic crash dump configuration. Explain how to configure an application to save a heap dump when it encounters an OutOfMemoryError and why the destination must be writable.
- VisualVM running process dump. Describe how to obtain a heap dump from a running process using VisualVM and open it for investigation.
- Command line heap capture. Describe how to use jps and jmap to save a heap dump from a running process when a profiler cannot connect.
- Heap dump contents and sensitivity. Explain what a heap dump captures, how it helps investigate memory allocation, and why it can contain sensitive information.
- Memory-heavy application type identification. Describe how to use the Objects view to identify application types worth investigating in a heap dump.
- Instance references and retention. Explain how inspecting an instance’s outgoing and incoming references helps investigate why it remains in memory.
- Reusable heap dump queries. Explain why OQL is useful when comparing heap dumps from different app versions.
- Instance queries and inspection. Describe how OQL selects instances of a type and what details can be inspected in the results.
- Values projections and conditions. Describe how OQL can return selected values, multiple values, and results matching a condition.
- Referrers and leak clues. Explain how querying referrers can reveal a potential memory leak, using the Product list example.
What it builds on
- Observing the CPU and memory usage (chapter 6) · Observing CPU and memory usage in VisualVM.
- Sampling to observe executing code (chapter 7) · Using sampling to obtain a high-level view of an app’s execution.
- Investigating code with a debugger (chapter 2) · Investigating code with a debugger
- Investigating issues with logs (chapter 5) · Investigating issues with logs
Section by section
Introduction
This introduction explains the consequences of poor memory management and why heap dumps can help investigate crashes.
- Memory allocation consequences: Explain how inefficient memory allocation can affect an app’s performance and continued execution.
Why it matters: Memory problems can affect both application availability and performance. - Heap dumps after crashes: Explain why analyzing a heap dump can help investigate a memory-related crash when profiling is unavailable.
Why it matters: A heap dump can provide evidence for an investigation when the crashed app cannot be profiled.
Key terms: memory allocation, memory management, GC, CPU, heap dump, allocated memory, profiling
11.1 Sampling and profiling for memory issues
This section uses VisualVM memory sampling and targeted profiling to investigate excessive object allocation and locate the code responsible.
- Memory pressure and monitoring: Explain how excessive memory use can contribute to slowness and how VisualVM can reveal it.
Why it matters: A memory problem can slow an app even when its own work consumes little CPU. - Sampling memory usage patterns: Describe how to sample memory usage and interpret the results to distinguish many objects from a few large objects.
Why it matters: Sampling provides a high-level view of which types occupy memory and why. - Targeted profiling expressions: Apply VisualVM memory profiling expressions to select a specific type, a package, or a package with its subpackages, and explain why the selection should be narrow.
Why it matters: A targeted profiling session focuses the investigation on the types already suspected. - Profiling allocation details: Explain when to follow sampling with profiling and what allocation details profiling provides.
Why it matters: Allocation details can connect a memory-heavy type to the code that creates its instances.
Key terms: garbage collector, Monitor tab, memory widget, Sampler tab, Memory button, Live Bytes, Live Objects, Memory settings, fully qualified name, package
11.2 Using heap dumps to find memory leaks
This section explains how memory leaks can crash a JVM application and why heap dumps help investigate memory issues when profiling is unavailable.
- Memory leak crash mechanism: Explain how a memory leak can lead to an application crash and how the JVM signals memory exhaustion.
Why it matters: Recognizing this sequence helps you connect a crash to a possible memory allocation problem. - Heap dump investigation role: Describe what a heap dump captures and why it is useful when an application cannot be profiled.
Why it matters: A heap dump offers evidence of memory state even when you cannot attach a profiler.
Key terms: memory leak, memory allocation, OutOfMemoryError, heap dump, heap memory, profiler
11.2.1 Obtaining a heap dump
This section explains how to obtain heap dumps automatically on a memory error, from a running process, or programmatically.
- Automatic crash dump configuration: Explain how to configure an application to save a heap dump when it encounters an OutOfMemoryError and why the destination must be writable.
Why it matters: A crash-time dump preserves heap information that can help you investigate a memory allocation failure. - VisualVM running process dump: Describe how to obtain a heap dump from a running process using VisualVM and open it for investigation.
Why it matters: You can capture heap information without waiting for the application to crash. - Command line heap capture: Describe how to use jps and jmap to save a heap dump from a running process when a profiler cannot connect.
Why it matters: The command-line route lets you capture a dump even when you cannot connect a profiler. - Programmatic heap dump generation: Describe the section's example of generating a heap dump programmatically through an application endpoint.
Why it matters: Programmatic generation offers another way to integrate heap dumps into application monitoring.
Key terms: OutOfMemoryError, -XX:+HeapDumpOnOutOfMemoryError, HeapDumpPath, heap dump, VisualVM, Heap Dump button, Monitor tab, jps, jmap, PID
11.2.2 Reading a heap dump
This section explains how to use VisualVM to examine a heap dump, identify memory-heavy application objects, and investigate their references.
- Heap dump contents and sensitivity: Explain what a heap dump captures, how it helps investigate memory allocation, and why it can contain sensitive information.
Why it matters: A heap dump can reveal both the objects behind a memory problem and sensitive data that needs protection. - VisualVM summary verification: Describe how to open and check a heap dump in VisualVM before investigating its objects.
Why it matters: Checking the summary first helps you avoid investigating the wrong file. - Memory-heavy application type identification: Describe how to use the Objects view to identify application types worth investigating in a heap dump.
Why it matters: Prioritizing application types helps turn a large heap dump into a manageable set of leads. - Instance references and retention: Explain how inspecting an instance’s outgoing and incoming references helps investigate why it remains in memory.
Why it matters: The objects referring to an instance can explain why it remains in memory. - Product retention case interpretation: Trace the reference relationship found for Product instances and explain what further investigation is needed to interpret it.
Why it matters: A suspiciously large collection is a lead, not by itself proof of a leak.
Key terms: heap dump, heap, allocated memory, VisualVM, summary view, Objects view, instances, occupied memory, Product, instance
11.2.3 Using the OQL console to query a heap dump
This section introduces using OQL in VisualVM to query heap dumps, compare app versions, and investigate potential memory leaks.
- Reusable heap dump queries: Explain why OQL is useful when comparing heap dumps from different app versions.
Why it matters: Reusable queries make it easier to compare memory details across versions. - Instance queries and inspection: Describe how OQL selects instances of a type and what details can be inspected in the results.
Why it matters: Selecting instances and inspecting their references gives you a starting point for investigating retained objects. - Values projections and conditions: Describe how OQL can return selected values, multiple values, and results matching a condition.
Why it matters: Selecting and filtering specific details makes query results more useful for an investigation. - Referrers and leak clues: Explain how querying referrers can reveal a potential memory leak, using the Product list example.
Why it matters: Finding objects that retain many instances can point you toward a memory leak. - Small practice heap dumps: Explain why small heap dumps are recommended when learning OQL.
Why it matters: A small practice dump helps you learn without waiting on slow queries over a large dump.
Key terms: OQL, heap dump, instances, fully qualified name, references, JSON, conditions, values, referrers(), referrals
11.3 Summary
Concepts explained in this chapter
- heap dumpA snapshot of the objects and related information present in a Java heap at a particular time.
- profilingThe practice of gathering information about a program's execution to understand its performance or behavior.
- samplingSampling is a profiling technique that observes selected parts of a program’s execution to reveal its behavior.
- Object Query LanguageOQL is a query language for retrieving and analyzing objects and references in a Java heap dump.
- memory usageA measure of the memory occupied by a process or application.
- profilerA software analysis tool that measures program execution, such as time spent in code, to help identify performance problems.
- memory leakA software problem in which an application fails to deallocate data it no longer needs, causing free memory to diminish over time.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
