如何忽略 LeakCanary 中的某些类?

问题描述 投票:0回答:3

有人能给我一个如何忽略 LeakCanary 中的某些类的有效示例吗?

我正在查看这个示例,以忽略 LeakCanary 中第三方库中的某些类,但我不知道将其放在应用程序中的何处。我把它放在我的应用程序类中,但这些变量和方法有错误:isInAnalyzerProcess、enableDisplayLeakActivity、application、androidWatcher

public class DebugExampleApplication extends ExampleApplication {
  protected RefWatcher installLeakCanary() {
    if (isInAnalyzerProcess(this)) {
      return RefWatcher.DISABLED;
    } else {
      ExcludedRefs excludedRefs = AndroidExcludedRefs.createAppDefaults().build();
      enableDisplayLeakActivity(application);
      ServiceHeapDumpListener heapDumpListener = new ServiceHeapDumpListener(application, DisplayLeakService.class);
      final RefWatcher refWatcher = androidWatcher(application, heapDumpListener, excludedRefs);
      registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
        public void onActivityDestroyed(Activity activity) {
          if (activity instanceof ThirdPartyActivity) {
              return;
          }
          refWatcher.watch(activity);
        }
        // ...
      });
      return refWatcher;
    }
  }
}
android memory-leaks leakcanary
3个回答
2
投票

感谢 CommonsWare,调用 LeakCanary 上的方法和变量是可行的。这是忽略 LeakCanary 中某些引用或活动的完整示例。看看评论:忽略引用和忽略活动。

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import com.squareup.leakcanary.AndroidExcludedRefs;
import com.squareup.leakcanary.DisplayLeakService;
import com.squareup.leakcanary.ExcludedRefs;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.squareup.leakcanary.ServiceHeapDumpListener;

public class MyApplication extends Application {

    // LeakCanary for memory leak detection
    private RefWatcher refWatcher;
    public static RefWatcher getRefWatcher(Context context) {
        MyApplication application = (MyApplication) context.getApplicationContext();
        return application.refWatcher;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        refWatcher = installLeakCanary();
    }


    /**
     * 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;
        }
    }

}

0
投票

除了现有的答案, LeakCanary 默认执行

watch(...)
所有 Activity, 您可以禁用它,例如:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <bool name="leak_canary_watcher_auto_install">false</bool>
</resources>

参见: https://square.github.io/leakcanary/changelog/#configuring-retained-object-detection


0
投票

现在从leakCanary 2.14开始,你必须使用ObjectWatcher而不是RefWatcher

   dependencies {
  implementation 'com.squareup.leakcanary:leakcanary-object-watcher-android:2.14'
}

// In shared code
interface MaybeObjectWatcher {
  fun watch(watchedObject: Any, description: String)

  object None : MaybeObjectWatcher {
    override fun watch(watchedObject: Any, description: String) {
    }
  }
}

// In debug code
class RealObjectWatcher : MaybeObjectWatcher {
  override fun watch(watchedObject: Any, description: String) {
    AppWatcher.objectWatcher.watch(watchedObject, description)
  }
}

参考链接。

https://square.github.io/leakcanary/upgrading-to-leakcanary-2.0/

© www.soinside.com 2019 - 2024. All rights reserved.