精选圈子榜单优站
编程综合
编程综合
技术
20关注
编程技术记录、分享 ,记录你的编程生活点点滴滴!

Android内存泄露工具Leakcanary的使用


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的内存泄露


  • 若文章侵犯了您的权益,请联系我们进行处理。

  • 2017-09-07
  • 2167阅读
评论