How to use Leak Canary

后端 未结 5 1645
悲&欢浪女
悲&欢浪女 2020-12-24 05:57

I know this is probably a dumb question, but I am pretty new at developing android, and I currently experiencing an OutOfMemoryError in my apps, which I have tried to debug

5条回答
  •  礼貌的吻别
    2020-12-24 06:21

    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 LeakCanary 2. Fix memory leak and confirm LeakCanary reports no leaks 3. 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.

提交回复
热议问题