Yes. There are a number of known memory leaks that have been fixed over time in AOSP as well as in manufacturer implementations. When such a leak occurs, there is little you can do as an app developer to fix it. For that reason, LeakCanary has a built-in list of known Android leaks to recognize, called Library Leaks (see Categorizing leaks).
If you find a new one, please create an issue (choose 🤖Leak in Android SDK / support library) and follow these steps:
You can confirm that LeakCanary starts correctly by filtering on the LeakCanary tag in Logcat:
$ adb logcat | grep LeakCanary D/LeakCanary: Installing AppWatcher
If you do not see Installing AppWatcher
in the logs, check your dependencies (./gradlew app:dependencies
) and make sure LeakCanary is there.
Note that LeakCanary is automatically disabled in tests (see LeakCanary test environment detection):
$ adb logcat | grep LeakCanary D/LeakCanary: Installing AppWatcher D/LeakCanary: JUnit detected in classpath, app is running tests => disabling heap dumping & analysis D/LeakCanary: Updated LeakCanary.config: Config(dumpHeap=false)
The default behavior is to store heap dumps in a leakcanary
folder under the app directory. If the app has been granted the android.permission.WRITE_EXTERNAL_STORAGE
permission, then heap dumps will be stored in a leakcanary-com.example
folder (where com.example
is your app package name) under the Download
folder of the external storage. If the app has not been granted the android.permission.WRITE_EXTERNAL_STORAGE
permission but that permission is listed in AndroidManifest.xml
then LeakCanary will show a notification that can be tapped to grant permission.
Sometimes the leak trace isn't enough and you need to dig into a heap dump with MAT or YourKit.
Here's how you can find the leaking instance in the heap dump:
leakcanary.KeyedWeakReference
.key
field.KeyedWeakReference
that has a key
field equal to the reference key reported by LeakCanary.referent
field of that KeyedWeakReference
is your leaking object.On Android, content providers are created after the Application instance is created but before Application.onCreate() is called. The leakcanary-object-watcher-android
artifact has a non exported ContentProvider defined in its AndroidManifest.xml
file. When that ContentProvider is installed, it adds activity and fragment lifecycle listeners to the application.
0. LeakCanary is a debug only library.
Update your dependencies to the latest SNAPSHOT (see build.gradle):
dependencies { debugImplementation 'com.squareup.leakcanary:leakcanary-android:{{ leak_canary.next_release }}-SNAPSHOT' }
Add Sonatype's snapshots
repository:
repositories { mavenCentral() maven { url 'https://s01.oss.sonatype.org/content/repositories/snapshots/' } }
LeakCanary was created and open sourced by @pyricau, with many contributions from the community.
The name LeakCanary is a reference to the expression canary in a coal mine, because LeakCanary is a sentinel used to detect risks by providing advance warning of a danger. Props to @edenman for suggesting it!