Scripts2026年5月17日·1 分钟阅读

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 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
LeakCanary Overview
通用 CLI 安装命令
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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产