A memory leak detection library for Android.

Apache-2.0

Quick Demo

1Add Dependencyterminal
Add implementation 'com.squareup.leakcanary:leakcanary-android:2.x' to your app's build.gradle file.
2Initialize in Applicationcode
class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        LeakCanary.install(this)
    }
}
3Run and Monitorphone

Launch the app and watch for LeakCanary notifications when memory leaks are detected.

Quick Start

Maven Coordinates
com.squareup.leakcanary:leakcanary-android

Setup Steps

  1. Add the LeakCanary dependency to your build.gradle file.
  2. Initialize LeakCanary in your Application class.
  3. Run your app and monitor for leak notifications.

Overview

LeakCanary is a memory leak detection library for Android that automatically identifies and reports memory leaks by dumping and analyzing the heap, helping developers improve app performance and stability.

Features

Automatic leak detection on Android
Heap dump analysis with detailed reports
Notification system for leak alerts
Integration with Android Studio for debugging
Supports both Java and Kotlin projects

Use Cases

Debugging memory leaks during development
Monitoring for leaks in production builds
Improving app performance by fixing memory issues

When Not to Use

Not recommended for production due to potential performance overhead
Avoid in resource-constrained or minimal apps where size is critical

Usage Examples

Basic Initialization
LeakCanary.config = LeakCanary.config.copy(dumpHeap = true)

Enable heap dumping in LeakCanary configuration.

Application Setup
class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        LeakCanary.install(this)
    }
}

Initialize LeakCanary in a custom Application class.

Common Pitfalls

  • Can increase app size and startup time
  • May produce false positives without proper configuration
  • Requires careful handling in multi-module projects

Integration

Works well with

Android Studio ProfilerSquare libraries like OkHttp and RetrofitKotlin coroutines for asynchronous operations
  • Test in various scenarios to avoid false positives
  • Consider using only in debug builds to minimize impact

Alternatives

Android Profiler

Built-in tool in Android Studio for real-time memory monitoring and analysis.

MAT (Memory Analyzer Tool)

Standalone Java heap analyzer for deep memory leak investigation.

FAQ

What is LeakCanary?

LeakCanary is an open-source library that automatically detects memory leaks in Android applications by analyzing heap dumps.

How does LeakCanary work?

It hooks into the Android lifecycle, dumps the heap when a leak is suspected, and provides a detailed report with leak traces.

Is LeakCanary suitable for production use?

It can be used in production, but it is often recommended for debug builds due to performance overhead and resource usage.

How do I disable LeakCanary in release builds?

Use build flavors or conditional initialization in your Application class to exclude it in release variants.

Does LeakCanary support Kotlin?

Yes, it is written in Kotlin and fully compatible with both Kotlin and Java projects.

Can I customize LeakCanary's behavior?

Yes, through configuration options in LeakCanary.config, such as enabling/disabling heap dumping or setting up custom leak detectors.

Maintenance & Health

  • Latest version not specified due to uncertainty
  • Logo URL not provided as exact CDN URL is unsure
  • SDK versions (min_sdk, compile_sdk, target_sdk) not specified for accuracy

Related libraries