安卓 内存 泄漏 工具 LeakCanary 使用

韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com

LeakCanary是Square开源了一个内存泄露自动探测神器 。这是项目的github仓库地址:https://github.com/square/leakcanary  。使用非常简单,在build.gradle中引入包依赖:

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

在Application中的onCreate方法中增加初始化代码:

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

集成后什么都不用做,按照正常测试,当有内存泄漏发生后,应用会通过系统通知栏发出通知,点击通知就可以进入查看内存泄漏的具体信息。在这里举个实践中的例子。把LeakCanary集成到项目中后,等App启动后一会,系统通知到了,点击通知,跳转到泄漏的详情页面进行查看。

原文地址:https://www.cnblogs.com/yue31313/p/7396993.html