ScriptsMay 17, 2026·3 min read

LeakCanary — Memory Leak Detection for Android

A memory leak detection library for Android that automatically detects and reports retained objects in debug builds, helping developers find and fix leaks before they ship.

Agent ready

This asset can be read and installed directly by agents

TokRepo exposes a universal CLI command, install contract, metadata JSON, adapter-aware plan, and raw content links so agents can judge fit, risk, and next actions.

Native · 98/100Policy: allow
Agent surface
Any MCP/CLI agent
Kind
Skill
Install
Single
Trust
Trust: Established
Entrypoint
LeakCanary Overview
Universal CLI install command
npx tokrepo install a388c402-520b-11f1-9bc6-00163e2b0d79

Introduction

LeakCanary is a memory leak detection library by Square that runs during development and automatically notifies you when an Activity, Fragment, or other object is retained in memory after it should have been garbage collected. It requires zero configuration to get started.

What LeakCanary Does

  • Automatically watches destroyed Activities, Fragments, and ViewModels for retention
  • Dumps the heap when a retained object is detected and analyzes the reference chain
  • Displays a notification with the leak trace showing the exact path preventing garbage collection
  • Groups duplicate leaks to reduce noise during development
  • Provides a built-in UI to browse all detected leaks in the app

Architecture Overview

LeakCanary registers lifecycle callbacks via an auto-installed ContentProvider. When an object is destroyed, ObjectWatcher holds a weak reference and checks after a delay if it was collected. If retained, it triggers a heap dump via Debug.dumpHprofData, then the Shark library parses the HPROF file to compute the shortest reference path from GC roots to the leaked object.

Self-Hosting & Configuration

  • Add only the debugImplementation dependency to keep it out of release builds
  • Customize watched objects by calling AppWatcher.objectWatcher.expectWeaklyReachable()
  • Configure the retention threshold via LeakCanary.config copy
  • Disable automatic heap dumps in CI by setting dumpHeap = false
  • Integrate with CI using the leakcanary-android-process artifact for separate process analysis

Key Features

  • Zero-config setup that works immediately on dependency addition
  • Shark heap analyzer is fast and runs on-device without a server
  • Leak trace clearly labels each reference as strong, soft, or weak
  • Detects leaks in custom lifecycle objects beyond standard Android components
  • Supports Plumber library for auto-fixing known Android framework leaks

Comparison with Similar Tools

  • Android Studio Profiler — manual heap inspection; LeakCanary automates detection during development
  • MAT (Eclipse Memory Analyzer) — powerful but requires manual HPROF export and desktop analysis
  • Perfetto — system-wide tracing focused on performance, not leak detection
  • Koom (Kwai) — production-grade OOM detection; LeakCanary targets debug builds

FAQ

Q: Does LeakCanary affect release builds? A: No, using debugImplementation ensures it is completely excluded from release APKs.

Q: Can I use LeakCanary in instrumentation tests? A: Yes, the leakcanary-android-instrumentation artifact integrates with AndroidX Test to fail tests on leaks.

Q: How long does heap analysis take? A: Typically 5-15 seconds on modern devices thanks to the optimized Shark parser.

Q: Does it detect native memory leaks? A: No, it only tracks Java/Kotlin object retention on the managed heap.

Sources

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets