xref: /aosp_15_r20/external/leakcanary2/docs/faq.md (revision d9e8da70d8c9df9a41d7848ae506fb3115cae6e6)
1*d9e8da70SAndroid Build Coastguard Worker# FAQ
2*d9e8da70SAndroid Build Coastguard Worker
3*d9e8da70SAndroid Build Coastguard Worker## Can a leak be caused by the Android SDK?
4*d9e8da70SAndroid Build Coastguard Worker
5*d9e8da70SAndroid Build Coastguard WorkerYes. 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](fundamentals-how-leakcanary-works.md#4-categorizing-leaks)).
6*d9e8da70SAndroid Build Coastguard Worker
7*d9e8da70SAndroid Build Coastguard WorkerIf you find a new one, please [create an issue](https://github.com/square/leakcanary/issues/new/choose) (choose **��Leak in Android SDK / support library**) and follow these steps:
8*d9e8da70SAndroid Build Coastguard Worker
9*d9e8da70SAndroid Build Coastguard Worker1. Provide the entire leak trace information (including metadata), and use backticks (`) for formatting.
10*d9e8da70SAndroid Build Coastguard Worker2. Read the AOSP source for that version of Android, and try to figure out why it happens. You can easily navigate through SDK versions by switching branches on the GitHub mirror: [android/platform_frameworks_base](https://github.com/android/platform_frameworks_base).
11*d9e8da70SAndroid Build Coastguard Worker3. Check if it happens on the latest version of Android, and otherwise use blame to find when it was fixed.
12*d9e8da70SAndroid Build Coastguard Worker4. If it's still happening, build a simple repro case.
13*d9e8da70SAndroid Build Coastguard Worker5. File an issue on [b.android.com](http://b.android.com) with the leak trace and the repro case. Please remember to follow up the issue when there are new responses. [b/176886060](https://issuetracker.google.com/issues/176886060) is a good example of effective and respectful communication.
14*d9e8da70SAndroid Build Coastguard Worker6. Create a PR in LeakCanary to update [AndroidReferenceMatchers](/leakcanary/api/shark-android/shark/-android-reference-matchers/). Optional: if you find a hack to clear that leak on previous versions of Android, feel free to document it.
15*d9e8da70SAndroid Build Coastguard Worker
16*d9e8da70SAndroid Build Coastguard Worker## How do I know if LeakCanary is running?
17*d9e8da70SAndroid Build Coastguard Worker
18*d9e8da70SAndroid Build Coastguard WorkerYou can confirm that LeakCanary starts correctly by filtering on the LeakCanary tag in Logcat:
19*d9e8da70SAndroid Build Coastguard Worker
20*d9e8da70SAndroid Build Coastguard Worker```
21*d9e8da70SAndroid Build Coastguard Worker$ adb logcat | grep LeakCanary
22*d9e8da70SAndroid Build Coastguard Worker
23*d9e8da70SAndroid Build Coastguard WorkerD/LeakCanary: Installing AppWatcher
24*d9e8da70SAndroid Build Coastguard Worker```
25*d9e8da70SAndroid Build Coastguard Worker
26*d9e8da70SAndroid Build Coastguard WorkerIf you do not see `Installing AppWatcher` in the logs, check your dependencies (`./gradlew app:dependencies`) and make sure LeakCanary is there.
27*d9e8da70SAndroid Build Coastguard Worker
28*d9e8da70SAndroid Build Coastguard WorkerNote that LeakCanary is automatically disabled in tests (see [LeakCanary test environment detection](recipes.md#leakcanary-test-environment-detection)):
29*d9e8da70SAndroid Build Coastguard Worker
30*d9e8da70SAndroid Build Coastguard Worker```
31*d9e8da70SAndroid Build Coastguard Worker$ adb logcat | grep LeakCanary
32*d9e8da70SAndroid Build Coastguard Worker
33*d9e8da70SAndroid Build Coastguard WorkerD/LeakCanary: Installing AppWatcher
34*d9e8da70SAndroid Build Coastguard WorkerD/LeakCanary: JUnit detected in classpath, app is running tests => disabling heap dumping & analysis
35*d9e8da70SAndroid Build Coastguard WorkerD/LeakCanary: Updated LeakCanary.config: Config(dumpHeap=false)
36*d9e8da70SAndroid Build Coastguard Worker```
37*d9e8da70SAndroid Build Coastguard Worker
38*d9e8da70SAndroid Build Coastguard Worker## Where does LeakCanary store heap dumps?
39*d9e8da70SAndroid Build Coastguard Worker
40*d9e8da70SAndroid Build Coastguard WorkerThe 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
41*d9e8da70SAndroid Build Coastguard Workerin 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.
42*d9e8da70SAndroid Build Coastguard Worker
43*d9e8da70SAndroid Build Coastguard Worker## How can I dig beyond the leak trace?
44*d9e8da70SAndroid Build Coastguard Worker
45*d9e8da70SAndroid Build Coastguard WorkerSometimes the leak trace isn't enough and you need to dig into a heap dump with [MAT](http://eclipse.org/mat/) or [YourKit](https://www.yourkit.com/).
46*d9e8da70SAndroid Build Coastguard Worker
47*d9e8da70SAndroid Build Coastguard Worker* Go to a heap analysis screen, click the overflow menu and select *Share Heap Dump*.
48*d9e8da70SAndroid Build Coastguard Worker
49*d9e8da70SAndroid Build Coastguard WorkerHere's how you can find the leaking instance in the heap dump:
50*d9e8da70SAndroid Build Coastguard Worker
51*d9e8da70SAndroid Build Coastguard Worker1. Look for all instances of `leakcanary.KeyedWeakReference`.
52*d9e8da70SAndroid Build Coastguard Worker2. For each of these, look at the `key` field.
53*d9e8da70SAndroid Build Coastguard Worker3. Find the `KeyedWeakReference` that has a `key` field equal to the reference key reported by LeakCanary.
54*d9e8da70SAndroid Build Coastguard Worker4. The `referent` field of that `KeyedWeakReference` is your leaking object.
55*d9e8da70SAndroid Build Coastguard Worker5. From then on, the matter is in your hands. A good start is to look at the shortest path to GC Roots (excluding weak references).
56*d9e8da70SAndroid Build Coastguard Worker
57*d9e8da70SAndroid Build Coastguard Worker## How does LeakCanary get installed by only adding a dependency?
58*d9e8da70SAndroid Build Coastguard Worker
59*d9e8da70SAndroid Build Coastguard WorkerOn 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.
60*d9e8da70SAndroid Build Coastguard Worker
61*d9e8da70SAndroid Build Coastguard Worker## How many methods does LeakCanary add?
62*d9e8da70SAndroid Build Coastguard Worker
63*d9e8da70SAndroid Build Coastguard Worker**0**. LeakCanary is a debug only library.
64*d9e8da70SAndroid Build Coastguard Worker
65*d9e8da70SAndroid Build Coastguard Worker## How do I use the SNAPSHOT version?
66*d9e8da70SAndroid Build Coastguard Worker
67*d9e8da70SAndroid Build Coastguard WorkerUpdate your dependencies to the latest SNAPSHOT (see [build.gradle](https://github.com/square/leakcanary/blob/main/build.gradle)):
68*d9e8da70SAndroid Build Coastguard Worker
69*d9e8da70SAndroid Build Coastguard Worker```gradle
70*d9e8da70SAndroid Build Coastguard Workerdependencies {
71*d9e8da70SAndroid Build Coastguard Worker  debugImplementation 'com.squareup.leakcanary:leakcanary-android:{{ leak_canary.next_release }}-SNAPSHOT'
72*d9e8da70SAndroid Build Coastguard Worker}
73*d9e8da70SAndroid Build Coastguard Worker```
74*d9e8da70SAndroid Build Coastguard Worker
75*d9e8da70SAndroid Build Coastguard WorkerAdd Sonatype's `snapshots` repository:
76*d9e8da70SAndroid Build Coastguard Worker
77*d9e8da70SAndroid Build Coastguard Worker```gradle
78*d9e8da70SAndroid Build Coastguard Workerrepositories {
79*d9e8da70SAndroid Build Coastguard Worker  mavenCentral()
80*d9e8da70SAndroid Build Coastguard Worker  maven {
81*d9e8da70SAndroid Build Coastguard Worker    url 'https://s01.oss.sonatype.org/content/repositories/snapshots/'
82*d9e8da70SAndroid Build Coastguard Worker  }
83*d9e8da70SAndroid Build Coastguard Worker}
84*d9e8da70SAndroid Build Coastguard Worker```
85*d9e8da70SAndroid Build Coastguard Worker
86*d9e8da70SAndroid Build Coastguard WorkerStatus of the snapshot build: [![Build Status](https://travis-ci.org/square/leakcanary.svg?branch=main)](https://travis-ci.org/square/leakcanary)
87*d9e8da70SAndroid Build Coastguard Worker
88*d9e8da70SAndroid Build Coastguard Worker## Who's behind LeakCanary?
89*d9e8da70SAndroid Build Coastguard Worker
90*d9e8da70SAndroid Build Coastguard WorkerLeakCanary was created and open sourced by [@pyricau](https://github.com/pyricau), with [many contributions](https://github.com/square/leakcanary/graphs/contributors) from the community.
91*d9e8da70SAndroid Build Coastguard Worker
92*d9e8da70SAndroid Build Coastguard Worker## Why is it called LeakCanary?
93*d9e8da70SAndroid Build Coastguard Worker
94*d9e8da70SAndroid Build Coastguard WorkerThe name **LeakCanary** is a reference to the expression [canary in a coal mine](http://en.wiktionary.org/wiki/canary_in_a_coal_mine), because LeakCanary is a sentinel used to detect risks by providing advance warning of a danger. Props to [@edenman](https://github.com/edenman) for suggesting it!
95*d9e8da70SAndroid Build Coastguard Worker
96*d9e8da70SAndroid Build Coastguard Worker## Who made the logo?
97*d9e8da70SAndroid Build Coastguard Worker
98*d9e8da70SAndroid Build Coastguard Worker* [@pyricau](https://github.com/pyricau) quickly made the [first version](https://github.com/square/leakcanary/blob/f0cc04dfbf3cca92a669f0d250034d410eb05816/assets/icon_512.png) of the logo. It was based on cliparts from [Android Asset Studio](http://romannurik.github.io/AndroidAssetStudio/icons-generic.html), mixed with the selection from a photo of a Canary. The exclamation mark means danger, the shield stands for protection, and the bird, well, is a canary.
99*d9e8da70SAndroid Build Coastguard Worker* [@romainguy](https://github.com/romainguy) turned the ugly logo into [a nice vector asset](https://github.com/square/leakcanary/pull/36).
100*d9e8da70SAndroid Build Coastguard Worker* [@flickator](https://github.com/flickator) designed [a much nicer logo](https://github.com/square/leakcanary/pull/1269) for LeakCanary 2.0!
101*d9e8da70SAndroid Build Coastguard Worker
102*d9e8da70SAndroid Build Coastguard Worker<p align="center">
103*d9e8da70SAndroid Build Coastguard Worker<img src="../images/logo-2.0.png" />
104*d9e8da70SAndroid Build Coastguard Worker</p>
105