android - 如何忽略 LeakCanary 中的某些类?

标签 android memory-leaks leakcanary

谁能给我一个如何从 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;
    }
  }
}

最佳答案

感谢 CommonsWare,可以调用 LeakCanary 上的方法和变量。这是忽略 LeakCanary 中某些引用或 Activity 的完整示例。查看评论:IGNORE Rreferences 和 IGNORE Activities。

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

}

关于android - 如何忽略 LeakCanary 中的某些类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34778533/

相关文章:

android - 使用 ArcGis 在 OneMap 上显示位置

java - 重复类条目的 TransformException

memory-leaks - 隐藏的内存泄漏

android - LeakCanary有回调吗?

android - LeakCanary 报告 InputMethodManager 中的泄漏

android - Sceneform - 如何修复泄漏的 SceneView?

java - 生成最终存档时出错 : java. io.IOException:无效的 keystore 格式

javascript - AngularJS 打开链接 if 条件

java - 在调查内存泄漏时要寻找哪一代?

c - libmagic 内存泄漏?