github地址:https://github.com/square/leakcanary
简介及使用教程:https://square.github.io/leakcanary/
分析时使用的是:leakcanary-android:2.5 版本。
使用
在app的build.gradle文件中添加依赖:
dependencies {
// debugImplementation because LeakCanary should only run in debug builds.
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.5'
}
不需要添加任何初始化代码。
如何确认LeakCanary是否已经开始工作了?在Logcat中看到有如下log时表示LeakCanary已经在运行了
D/LeakCanary: LeakCanary is running and ready to detect memory leaks.
为什么在build.gradle文件中添加依赖之后不需要在Application中初始化就可以开始检测内存泄露?
LeakCanary可以检测哪些类型的对象
官方文档说了:
LeakCanary automatically detects leaks of the following objects:
- destroyed
Activity
instances - destroyed
Fragment
instances - destroyed fragment
View
instances - cleared
ViewModel
instances
如果想要添加检测其他对象,可以使用:
AppWatcher.INSTANCE.getObjectWatcher().watch()
参考: