大飞

大飞 关注TA

挑战一切!

大飞

大飞

关注TA

挑战一切!

  •  世界的顶端
  • 混口饭吃
  • 写了333,609字

最近回复

该文章投稿至Nemo社区   Android  板块 复制链接


Android内存泄露工具Leakcanary的使用

发布于 2017/09/07 14:58 1,622浏览 2回复 813

GitHub地址

1、引入依赖

 dependencies {
   debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.2'
   releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.2'
 }

2.application中初始化

public static RefWatcher getRefWatcher(Context context) {
MyApplication application = (MyApplication) context.getApplicationContext();
return application.refWatcher;
}

private RefWatcher refWatcher;
oncreate方法中
private void initlLeakcanary() {
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
refWatcher= LeakCanary.install(this);
}

3、在activity和fragment的onDestroy

//内存泄露监控
RefWatcher refWatcher = MyApplication.getRefWatcher(mContext);
refWatcher.watch(this);

Screenshot_2017-09-07-14-59-19-585_com

如上是handler的内存泄露

点赞(0)
点了个评