How to use Leak Canary How to use Leak Canary java java

How to use Leak Canary


The nice thing about leak canary is how automated it works.By default, it already "watches" for activities that are not being properly GCed. So out of the box, if any activity is leaking you should receive the notification.

On my project I've added an extra method on the Application like this:

public class ExampleApplication extends Application {    public static ExampleApplication instance;    private RefWatcher refWatcher;    @Override    public void onCreate() {        super.onCreate();        instance = this;        refWatcher = LeakCanary.install(this);    }    public void mustDie(Object object) {        if (refWatcher != null) {            refWatcher.watch(object);        }    }}

so the important stuff with garbage collection and memory leak and canary is to know when stuff should be collected and ask that item to be watched.

For for example we're using a "base fragment" with the following code:

@Overridepublic void onDestroy() {    super.onDestroy();    ExampleApplication.instance.mustDie(this);}

this way LeakCanary is trying to check if any fragment is leaking memory.

So for you to further implement on your app, you could/should on tasks or instances that you know it should be garbage collected but you think it might not be, and you're not sure where, you can call that too: ExampleApplication.instance.mustDie(object);

and then you MUST run the application and rotate the device and force the leak to happen, so leak canary can grab/analyse the stack trace and give you valuable information on how to fix it.

I hope it helps.


Posting this answer because all other answers are no longer needed and there's a better way to find leaks on your app using Leak Canary 2.

Just add below dependency to your gradle. No more code needed.

debugImplementation 'com.squareup.leakcananry:leakcanary-android:2.7'


I had the same question about how to use LeakCanary. I just wanted to see a basic example of how to launch it and see my first path to a leaked object.

How to Use LeakCanary

Here is a basic example of how to LeakCanary working:

How to Use LeakCanary (4 minutes 13 seconds)

One of the issues I had to overcome was figuring out that I had to launch the app in regular run-mode as opposed to debug mode. I also had to deviate from the basic instructions and set my application-level build.gradle file as follows:

dependencies {  debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'  releaseImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'}

I think debugImplementation didn't work for me because LeakCanary ignores leak detection when debugging. Although LeakCanary says it provides the "no-op" version of its library for release, since debug didn't work for me, I changed the releaseImplementation above from the recommended com.squareup.leakcanary:leakcanary-android-no-op:1.5.4 to com.squareup.leakcanary:leakcanary-android:1.5.4.

As you can see in the video, another issue I had to deal with was giving write-access to LeakCanary. I swiped down and saw a notification from LeakCanary saying it had failed to obtain write-access (more info). I never saw the permission request. So in one case (not shown in video), I gave my application write access by going to the Settings app, finding my app (as opposed to the app called "Leak" that LeakCanary installs), and turning on write-access. In the video I didn't need to do that because I gave permission by responding to the notification. Then while using my app I check periodically for new notifications (by swiping down). I saw messages like "XYZActivity leaked 217 KB". I tapped on that and it took me into that Leak app.

Also, I noticed that Sometimes it could take up to a few minutes for the analysis to complete and show a memory leak notification on your phone.

How to verify a memory leak fix using LeakCanary

Now that I've fixed some of my memory leaks, I use LeakCanary to verify the fix. However, if LeakCanary doesn't report anything I don't necessarily believe that's because my leak is fixed. It could just be LeakCanary is broken.

How to verify a memory leak fix using LeakCanary (16 minutes 34 seconds)

Process for Verifying Memory Leak with LeakCanary:1. Confirm memory leak exists using LeakCanary2. Fix memory leak and confirm LeakCanary reports no leaks3. Revert your fix and confirm LeakCanary reports the leak again

Since LeakCanary shows very little status information when it is working, it's hard to know if it's doing anything at all. This has led me to think I had fixed a memory leak when, in fact, I hadn't. The three steps above are the best way I've found to verify a memory leak fix using LeakCanary.