Kotlin Android LeakCanary内存泄漏检测实战

在Kotlin Android应用中使用LeakCanary检测内存泄漏的步骤如下:

1. 添加依赖

在模块的build.gradle文件中添加LeakCanary依赖:

kotlin 复制代码
dependencies {
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.12' // 使用最新版本
}

2. 自动初始化(无需额外代码)

LeakCanary 2.x及以上版本会自动初始化,无需手动配置。确保应用启动时加载了Application类即可。

3. 制造内存泄漏(测试)

创建存在内存泄漏的Activity:

kotlin 复制代码
class LeakyActivity : AppCompatActivity() {
    companion object {
        var leakedContext: Context? = null // 静态引用导致泄漏
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        leakedContext = this // 错误:静态变量持有Activity实例
    }
}

4. 触发检测

运行应用,进入LeakyActivity后退出。LeakCanary会自动检测泄漏,并在通知栏提示。

5. 分析报告

点击通知查看泄漏轨迹,报告会显示泄漏对象的引用链。例如:

复制代码
┬───
│ GC Root: System class
│
├─ LeakyActivity class
│    Leaking: NO (a class is never leaking)
│    ↓ static LeakyActivity.leakedContext
│                         ~~~~~~~~~~~~~~
╰→ LeakyActivity instance
     Leaking: YES (ObjectWatcher was watching this)

报告指出leakedContext静态变量导致泄漏。

6. 修复泄漏

onDestroy中清除引用:

kotlin 复制代码
override fun onDestroy() {
    super.onDestroy()
    leakedContext = null // 释放引用
}

7. 自定义配置(可选)

在自定义Application类中调整LeakCanary:

kotlin 复制代码
class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        LeakCanary.config = LeakCanary.config.copy(
            retainedVisibleThreshold = 3, // 触发堆转储前保留的可见实例数
            referenceMatchers = listOf(
                IgnoredReferenceMatcher(
                    pattern = "com.example.IgnoredClass" // 忽略特定类
                )
            )
        )
    }
}

8. 高级场景

  • Fragment监测:LeakCanary默认支持Fragment销毁后的泄漏检测。

  • 自定义对象监控 :手动观察对象:

    kotlin 复制代码
    val watchedObject = SomeObject()
    LeakCanary.watch(
        watchedObject,
        description = "SomeObject should be garbage collected"
    )

9. 注意事项

  • 性能影响 :仅在debug构建中使用,避免影响生产环境性能。
  • 及时处理泄漏:修复报告中优先级高的泄漏,避免累积问题。
  • 持续集成 :可通过LeakCanary的JUnit规则在自动化测试中检测泄漏。

10. 常见泄漏场景及修复

  • 静态Context引用 :改用ApplicationContext或弱引用。
  • 未注销监听器 :在生命周期结束时注销(如onDestroy)。
  • Handler/Runnable延迟任务 :使用WeakReferenceView.postDelayed并清理回调。

通过以上步骤,可有效利用LeakCanary识别和修复内存泄漏,提升应用稳定性和性能。

相关推荐
阿巴斯甜1 天前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker1 天前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq95271 天前
Andorid Google 登录接入文档
android
黄林晴1 天前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab2 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿2 天前
Android MediaPlayer 笔记
android
Jony_2 天前
Android 启动优化方案
android
阿巴斯甜2 天前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android
张小潇2 天前
AOSP15 Input专题InputReader源码分析
android
_小马快跑_2 天前
Kotlin | 协程调度器选择:何时用CoroutineScope配置,何时用launch指定?
android