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);
如上是handler的内存泄露