OOM第三方监控控件LeakCanary使用详解
1.首先配置build.gradle:
dependencies {
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.2'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.2'
}
2.在Application中加入如下代码:可以加在主工程的Application中,也可以加在Application继续的上级Application中,位置好像没那么讲究
publicclassLeakApplicationextendsApplication {
@OverridepublicvoidonCreate() {
super.onCreate();
//这个if可以不要,加了弹出通知慢很多,不加的话不确定有没有其它影响
/*if (LeakCanary.isInAnalyzerProcess(this)){
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}*/
LeakCanary.install(this);
}
}
3.以上就已经添加好了(只是监听Activity,如果想监听Fragment或其它,请参考其它文章),接下来就是使用
4.正常使用,如果发生了OOM,这时会闪出一个提示框,提示内容为:“Dumping memory,app will freeze.Brrrr.”
过一会后,内存泄漏信息就会通过Notification展示出来。
可以这样理解:
第一部分(ProtectAccessibilityManager类的mAccessibilityManager变量中的defaultLauncherFactory)
引用第二部分(DefaultLauncherFactory类中的mContext变量),
导致第三部分(GuideSettingsActivity类的实例instance)泄露.