How can I exclude a class from LeakCanary?

天大地大妈咪最大 提交于 2019-12-10 15:58:33

问题


I'm doing this:

ExcludedRefs excludedRefs = AndroidExcludedRefs.createAppDefaults()
                .clazz("androidx.lifecycle.ReportFragment")
                .reason("Very annoying report fragment leak that isn't a leak apparently")
                .alwaysExclude()
                .build();
        LeakCanary
                .refWatcher(context)
                .listenerServiceClass(DisplayLeakService.class)
                .excludedRefs(excludedRefs)
                .watchDelay(10, TimeUnit.SECONDS)
                .buildAndInstall();

And yet I'm still getting the ReportFragment was never GCed but no leak found.

I also get that for one of my activities, no I idea what to do about these no leak found messages.

Edit: current using LeakCanary 1.6.3


回答1:


I found this; I thought I might try it

This is working as expected. We can't know if a leak is excluded prior to performing the heap dump and analysis. Once we've done that, the default behavior is to display a notification with [Excluded] as a prefix. This lets users know that LeakCanary is done. If there was no feedback at all, you wouldn't have any way to know whether leak canary is done or not. This is arguably a better user experience than no feedback at all.

You can customize this behavior by providing your own subclass of com.squareup.leakcanary.AbstractAnalysisResultService rather than using the default com.squareup.leakcanary.DisplayLeakService




回答2:


 /**
 * Excluding known memory leaks from third party libraries
 * @return
 */
protected RefWatcher installLeakCanary() {
    if (LeakCanary.isInAnalyzerProcess(this)) {
        return RefWatcher.DISABLED;
    } else {

        // IGNORE References: Update or add reference class and context name in instanceField
        ExcludedRefs excludedRefs = AndroidExcludedRefs.createAppDefaults()
                .instanceField("com.example.third.party.TheirClassOne", "context")
                .instanceField("com.example.third.party.TheirClassTwo", "context")
                .build();

        LeakCanary.enableDisplayLeakActivity(this);
        ServiceHeapDumpListener heapDumpListener = new ServiceHeapDumpListener(this, DisplayLeakService.class);
        final RefWatcher refWatcher = LeakCanary.androidWatcher(this, heapDumpListener, excludedRefs);
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                //IGNORE Activities: Update or add the class name here to ingore the memory leaks from those actvities
                if (activity instanceof ThirdPartyOneActivity) return;
                if (activity instanceof ThirdPartyTwoActivity) return;
                if (activity instanceof ThirdPartyThreeActivity) return;
                refWatcher.watch(activity);
            }

            @Override
            public void onActivityResumed(Activity activity) {

            }
        });
        return refWatcher;
    }
}


来源:https://stackoverflow.com/questions/54598742/how-can-i-exclude-a-class-from-leakcanary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!