🗂️ Heap Dumping Explained - LeakCanary's Bold Move

Heap Dumping Explained — Why LeakCanary Freezes Your App to Find the Truth

Learn what a heap dump is, when LeakCanary triggers one, why it briefly pauses your app, and how the resulting .hprof snapshot enables Shark to find real Android memory leaks.

In the previous part of this series, we explored ObjectWatcher, the component that keeps an eye on destroyed Activities, Fragments, Views, and custom objects. But ObjectWatcher does not prove a leak on its own. It only tells LeakCanary that some objects still look suspiciously alive after they should have been garbage collected.

That is the moment when LeakCanary makes its boldest move: it captures a heap dump.

Heap dumping is the turning point in LeakCanary’s workflow. It briefly freezes the app, records a binary snapshot of memory, writes that snapshot to disk as a .hprof file, and passes it to Shark for analysis. [page:1]

Table of Contents

1. What Is a Heap Dump?

A heap dump is a binary snapshot of the JVM heap at a specific moment in time. It captures the objects currently in memory, the classes they belong to, the fields they hold, the references between them, and the GC roots that keep object graphs reachable. [page:1]

If you want a practical mental model, think of it as a crime-scene photo for memory. Instead of guessing what might have happened, you capture the exact state of the heap and inspect the evidence afterward. [page:1]

2. Why LeakCanary Dumps the Heap

LeakCanary does not dump the heap for every watched object. That would be too expensive and too noisy. Instead, it waits until enough suspicious retained objects accumulate before taking a snapshot. [page:1]

This is important because ObjectWatcher only tells you that an object was expected to die but did not. A heap dump provides the ground truth needed to answer the real question: what is retaining this object, and through which reference path? [page:1]

Without heap dumping, LeakCanary would only have suspicion. With heap dumping, it has evidence. [page:1]

3. When a Heap Dump Is Triggered

The current article explains that LeakCanary triggers a heap dump when retained objects exceed specific thresholds: 5 retained objects while the app is visible, and 1 retained object while the app is backgrounded. [page:1]

Those thresholds are deliberate. They reduce unnecessary dumps during active app usage, while still allowing aggressive checking when the app is backgrounded and a pause is less disruptive. [page:1]

HeapDumpTrigger
  .onRetainedObjectsDetected {
    heapDumper.dumpHeap()
  }

The exact implementation details may evolve over time, but the conceptual model stays the same: suspicious retained objects cross a threshold, and LeakCanary escalates from observation to snapshot capture. [page:1]

4. What Happens During Heap Dumping

When the threshold is reached, LeakCanary briefly pauses the app and writes the contents of the heap into a .hprof file. After that, normal execution resumes and the file is handed to Shark for analysis. [page:1]

This brief pause is not a bug in LeakCanary. It is a necessary cost of getting a consistent memory snapshot that can be analyzed accurately. [page:1]

Retained objects detected → Threshold reached → Heap dump triggered → .hprof written → Shark analysis begins

5. What a Heap Dump Contains

According to the source article, a heap dump contains all objects currently in memory, their classes and fields, the references between objects, and the GC roots that act as entry points into the object graph. [page:1]

  • Objects: Every live object visible in the snapshot. [page:1]
  • Class metadata: The type information needed to interpret fields. [page:1]
  • Reference edges: The links that show who points to whom. [page:1]
  • GC roots: Starting points Shark uses to trace why an object is still reachable. [page:1]

This is exactly why Shark can later reconstruct retention paths and produce a meaningful LeakTrace. A heap dump is not a summary; it is the raw memory graph. [page:1]

6. Trade-offs and Performance Cost

The source article explicitly calls out three trade-offs: app freeze, large file size, and performance impact. [page:1]

  • App freeze: Heap dumping pauses the app for a short time. [page:1]
  • Large files: Bigger heaps create larger .hprof outputs. [page:1]
  • Debug-only practicality: Because of the cost, this is best suited to debug builds rather than production. [page:1]

This trade-off is one of the most important design choices in LeakCanary. It avoids permanent overhead during normal development and only pays the cost when evidence is needed. [page:1]

7. Why Heap Dumps Are Still Worth It

Even with the pause and storage cost, the article argues that heap dumps are worth it because they provide ground truth, enable Shark’s analysis pipeline, and help developers fix leaks before they hit production. [page:1]

That is the key engineering principle here: a short, intentional interruption in debug mode is better than shipping a slow, leaky, crash-prone app to users. [page:1]

  • No guesswork: You inspect the real heap state. [page:1]
  • Actionable analysis: Shark can find shortest paths, build leak traces, and compute retained size from the snapshot. [page:1]
  • Early prevention: Leaks can be fixed during development instead of after production damage appears. [page:1]

8. End-to-End Workflow

This article fits into the larger LeakCanary story as the bridge between detection and diagnosis:

  1. ObjectWatcher suspects that destroyed objects are still retained.
  2. LeakCanary checks whether retained objects cross a threshold. [page:1]
  3. A heap dump is captured as a .hprof file. [page:1]
  4. Shark parses that file and reconstructs the object graph.
  5. LeakCanary presents a leak trace that developers can act on.

In other words, heap dumping is the handoff point between suspicion and proof. [page:1]

9. Analyze Leak Reports Faster with LeakLens

Once LeakCanary and Shark generate reports, interpreting complex traces can still take time. LeakLens complements this workflow by analyzing LeakCanary reports, explaining retention paths, and suggesting fixes directly inside Android Studio. [page:1]

Explore LeakLens

10. What to Read Next

The original article points readers to the next part of the series: Shark Heap Analysis, where the raw .hprof snapshot becomes a graph, a path, and an actionable leak report. [page:1]

FAQ

What is a heap dump in Android?

A heap dump is a binary snapshot of the JVM heap that captures live objects, fields, references, and GC roots at a specific moment. [page:1]

When does LeakCanary trigger a heap dump?

The article states that LeakCanary triggers a dump at 5 retained objects while the app is visible and 1 retained object while the app is backgrounded. [page:1]

Why does LeakCanary freeze the app?

It briefly pauses the app so it can write a consistent .hprof snapshot to disk for Shark to analyze accurately. [page:1]

Why not dump the heap all the time?

Because heap dumping has a performance cost: it pauses the app, creates large files, and is best suited for debug builds rather than production use. [page:1]

This guide is part of the Android Quality Suite.

Comments

Featured Articles

Optimize Jetpack Compose: Performance & Best Practices

Play Store Uploads with Fastlane Supply - 4

Building a Production-Ready Kotlin Multiplatform Platform Kit: Lessons from My BlrKotlin x InMobi Talk

Android Device Security: Sandboxing, Rooting, and Attestation Explained

From ‘Master’ to ‘Main’: The Meaning Behind Git’s Naming Shift