LeakCanary for Kotlin: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

LeakCanary for Kotlin: Complete Guide with Examples

In Android development, memory management is one of the most critical tasks. Memory leaks occur when an object that is no longer needed by the app continues to be held in memory due to implicit references. This leads to increased memory consumption, frequent GC (Garbage Collection) pauses, lag, and ultimately OutOfMemoryError crashes. LeakCanary is an open-source library from Square that automatically detects memory leaks in Android apps built with Java and Kotlin.

Why do you need it? Manually debugging leaks is a complex and time-consuming process that requires deep JVM knowledge, heap dump analysis, and profilers like Android Studio Profiler or MAT. LeakCanary handles all of this for you: it automatically tracks the lifecycle of Activities, Fragments, ViewModels, and other components. If any of these aren't garbage-collected after being destroyed, the library creates a heap dump, analyzes it, and delivers a ready-to-read report showing the exact reference chain causing the leak. This lets developers quickly find and fix issues without spending hours on manual analysis.

LeakCanary integrates into your app as a library and runs in the background. It doesn't affect the user experience but provides developers (in debug builds) with notifications and detailed logs. Starting with version 2.x, the library was fully rewritten in Kotlin, becoming more performant and easier to use. It supports the latest Android architectural components (Jetpack) and allows you to customize the leak detection process.

Installation

LeakCanary is distributed via Maven Central. To install it, add the dependency to your module's build.gradle.kts (or build.gradle) file.

Important: LeakCanary is intended only for debug builds. Never include it in your release version, as it can negatively impact performance and security.

// build.gradle.kts (Module :app)
dependencies {
    // debugImplementation — the library is only available in debug builds
    debugImplementation("com.squareup.leakcanary:leakcanary-android:2.14")
    
    // For Jetpack Navigation support (optional)
    debugImplementation("com.squareup.leakcanary:leakcanary-android-navigation:2.14")
    
    // For Process Lifecycle support (optional)
    debugImplementation("com.squareup.leakcanary:leakcanary-android-process:2.14")
}

After syncing Gradle, LeakCanary automatically registers its ContentProvider when the app starts. No additional code initialization is required.

Quick Start

The minimal working example is simply installing the library. After that, LeakCanary will automatically detect leaks.

1. Install the library as described above.

2. Run your app in debug mode.

3. Perform an action that might cause a leak (e.g., rotate the screen if there's an Activity leak).

4. If a leak is detected, you'll see a system notification:

LeakCanary Notification

5. Tap the notification to open a detailed report (Heap Dump Info).

Here's example code that creates a leak for testing:

// Example leak: static reference to an Activity
class LeakyActivity : AppCompatActivity() {
    companion object {
        // Static reference prevents the Activity from being GC'd
        var leakedActivity: Activity? = null
    }

    override fun onCreate(savedInsta                    

Recommendations