【问题标题】:How can I exclude a class from LeakCanary?如何从 LeakCanary 中排除一个类?
【发布时间】:2019-07-03 01:39:52
【问题描述】:

我正在这样做:

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();

但我仍然收到ReportFragment was never GCed but no leak found

我的一项活动也得到了它,我不知道如何处理这些no leak found 消息。

编辑:当前使用 LeakCanary 1.6.3

【问题讨论】:

  • 我实际上开始认为我不应该阻止它,因为这只是它向我展示的课程,但我的 Activity 正在扩展它。我的主要测试手机通常从不向我显示泄漏痕迹,但我在另一部手机上尝试过,看起来我的一个活动正在泄漏。
  • 该活动中有一些后台任务,这就是它泄漏的原因

标签: android leakcanary


【解决方案1】:

我找到了这个;我想我可以试试

这是按预期工作的。 在执行堆转储和分析之前,我们无法知道是否排除了泄漏。完成此操作后,默认行为是显示带有 [Excluded] 作为前缀的通知。这让用户知道 LeakCanary 已经完成。如果根本没有反馈,您将无法知道泄漏金丝雀是否完成。这可以说是比没有反馈更好的用户体验。

您可以通过提供自己的 com.squareup.leakcanary.AbstractAnalysisResultService 子类而不是使用默认的 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-29
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多