LeakCanary: memory leak detection built for Android devs
LeakCanary is Square's memory leak detection library for Android — it catches a retained Activity, Fragment, or View before it becomes a production OutOfMemoryError. It watches objects that should be garbage collected, dumps the heap when one lingers, and traces the reference chain back to what's holding it. For a Kotlin or Java project, adding it in development is close to a no-brainer — cheap, and the trace is specific enough to act on.
What is LeakCanary?
LeakCanary is an open-source Android library from Square that detects memory leaks while you're developing an app, not after it ships. It hooks into the Android lifecycle to watch objects — a destroyed Activity, a cleared Fragment — that the garbage collector should reclaim. When one lingers, LeakCanary dumps the heap and traces the reference chain from a GC root down to the object that's stuck.
Key features of LeakCanary
- ✓Automatic detection of retained Activity, Fragment, and View instances while you develop, no manual profiling session needed
- ✓Heap dumping and hprof analysis built in, so there's no separate analyzer to wire up
- ✓Leak trace output that shows the exact reference chain from a GC root to the leaking object
- ✓Works the same way in Kotlin and Java Android projects, since it hooks into the Android lifecycle, not the language
- ✓Apache-2.0 license, so it drops into a commercial app's debug build without a licensing conversation
- ✓Ships as a Gradle dependency — nothing to run as a separate service
When to use LeakCanary
- •Debugging an app whose memory usage keeps climbing over a long session
- •Catching a leaked Activity or Fragment right after refactoring navigation or lifecycle code
- •Running continuously during development so a leak surfaces before QA or a production crash does
- •Checking whether a third-party SDK or custom View is holding a reference past its Activity's lifecycle
Adding LeakCanary to your Android project
LeakCanary is added as a Gradle dependency to your app module — there's no account, service, or separate install step. This source doesn't include the exact dependency coordinates or current version number, so pull the precise Gradle line from the project's own docs at square.github.io/leakcanary rather than guessing at a version here. Once it's in the build, most apps need no further setup code to start seeing leak traces.
How LeakCanary works
LeakCanary watches for objects that are expected to be garbage collected — a destroyed Activity or Fragment, a cleared ViewModel, a View removed from its parent. If one of those objects is still around after enough time to be collected, LeakCanary triggers a heap dump and writes it to an hprof file. It then parses that dump and computes the shortest path from a GC root to the retained object — the leak trace — which points at the specific reference in your code keeping it alive. You see the result as a notification during development. No separate profiler session needed.
Strengths
- ✓Runs automatically once it's in the project — no manual profiling session to remember to start
- ✓Leak trace points at the specific reference holding the object, not just a memory-usage graph
- ✓Apache-2.0 license keeps it usable in a commercial app without extra legal review
- ✓Same detection whether the app is Kotlin, Java, or a mix of both
Limitations of LeakCanary
- △This source doesn't list supported Android API version ranges — confirm compatibility with the project's own docs before adding it to an app on an older API level
- △Heap dumping and analysis carry real overhead, which is why it belongs in a debug build, not a release one
- △Exact install steps and current version number aren't in this GitHub summary, so first-time setup needs the project's own documentation
Alternatives for Android memory analysis
Frequently asked questions
LeakCanary is built to run in a debug build during development, not the release build a user installs — the heap-dumping and analysis overhead isn't something you want shipping to production. Development-only use is the standard way this kind of tool is set up.
The GitHub source used for this review doesn't list specific minimum or maximum Android API levels. LeakCanary targets current Android development, so check the project's own documentation at square.github.io/leakcanary for the exact supported range before you rely on it.
LeakCanary is licensed under Apache-2.0, a permissive open-source license that allows commercial use in a paid or closed-source Android app without royalties. You still need to follow the license's standard attribution terms.
LeakCanary watches objects that should be garbage collected once they're no longer needed, such as a destroyed Activity. If one is still in memory after it should've been collected, LeakCanary dumps the heap to an hprof file and analyzes it to trace the exact chain of references keeping that object alive.
LeakCanary watches common Android leak sources, including Activities, Fragments, and Views that outlive their expected lifecycle, plus any other object your code registers for watching. The result is a leak trace showing which reference is holding each one in memory.
LeakCanary works with both Kotlin and Java Android projects because it hooks into the Android framework's lifecycle rather than depending on a specific JVM language. Kotlin is the library's own implementation language, per its GitHub metadata, but that doesn't limit which language your app is written in.
The problem it solves
A memory leak in an Android app is quiet until it isn't — an app that holds onto a destroyed Activity or Fragment slowly eats available memory across a session, and the usual failure mode is a production OutOfMemoryError with a stack trace that doesn't say which object caused it. Catching that during development, before it reaches a crash report, is the specific gap LeakCanary fills: it names the reference that's leaking while you're still writing the code that leaked it.
Who should try it — and who should skip
A team shipping a production Android app in Kotlin or Java should have LeakCanary in the debug build — the setup cost is low next to the cost of chasing an OutOfMemoryError crash report with no leak trace attached. Skip it, or don't lean on it alone, if you're building a library or SDK meant to run inside someone else's app; the tool is built around your own app's development loop, not something you ship to consumers. It's also not the right tool for a broad performance or CPU regression — that's a profiler's job, not a leak detector's.
Related repositories
Still deciding about leakcanary?
One click hands the question to an AI along with this page — see what it says about leakcanary.
