最近看到看到郭大神的微信文章和第二行代码,一则巩固基础,二则查漏补缺。最最最重要的当属看了一篇内存泄漏的文章,顿觉醍醐灌顶。最近使用LeakCanary,不用不知道,一用吓一跳,公司项目中momory leak 多如牛毛,渗出一身冷汗,难怪偶尔遭遇莫名OOM。废话不多说,直入正题,LeakCanary使用非常简单。
1. open github, 地址 https://github.com/square/leakcanary
2. 测试阶段就在 gradle 的 dependencies闭包中加入如下内容
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
3. 最后在Application 中初始化,然后运行项目,就会在 同时安装的 Leaks 中捕获出现的leak,好用之极,感谢这个开源项目的幕后大神,强烈推荐!!
public class ExampleApplication extends Application { @Override public void onCreate() { super.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); // Normal app init code... } }
ps: 喜欢的话可以查看郭神的那篇内存分析的文章,http://mp.weixin.qq.com/s/nIIzHv_sqZom_y2nblCjJQ
本文介绍如何使用LeakCanary检测内存泄漏,并提供了一个简单的示例。通过在Gradle文件中添加依赖并在Application中初始化LeakCanary,可以轻松地在测试阶段捕获并解决内存泄漏问题。
2万+

被折叠的 条评论
为什么被折叠?



