Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsMay 17, 2026·3 min de lecture

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.

Prêt pour agents

Cet actif peut être lu et installé directement par les agents

TokRepo expose une commande CLI universelle, un contrat d'installation, le metadata JSON, un plan selon l'adaptateur et le contenu raw pour aider les agents à juger l'adaptation, le risque et les prochaines actions.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
LeakCanary Overview
Commande CLI universelle
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

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires