Github项目解析(三)-->Android内存泄露监测之leakcanary
来源:互联网 发布:深入浅出avr单片机pdf 编辑:程序博客网 时间:2024/05/17 22:35
转载请标明出处:一片枫叶的专栏
(一)什么是内存泄露
Java内存泄漏指的是进程中某些对象(垃圾对象)已经没有使用价值了,但是它们却可以直接或间接地引用到gc roots导致无法被GC回收。无用的对象占据着内存空间,使得实际可使用内存变小,形象地说法就是内存泄漏了。
(二)什么是leakcanary
LeakCanary 是一个square开源的在debug版本中检测内存泄漏的java库;
其github地址:https://github.com/square/leakcanary
(三)如何使用leakcanary检测内存泄露
在leakcanary的github地址中已经对如何使用做了相关的说明,这里简单介绍一下:
1)在android studio的build.gradle中引用leakcanary
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
2)自定义Application,并在onCreate方法中执行以下代码:
public class ExampleApplication extends Application { public RefWatcher refWatcher = null; @Override public void onCreate() { super.onCreate(); refWatcher = LeakCanary.install(this); }}
3)在activity或者是fragment中的onCreate方法中执行:
//在自己的应用初始Activity中加入如下两行代码RefWatcher refWatcher = ExampleApplication.getRefWatcher(this);refWatcher.watch(this);
这样我们就可以接受检测该Activity中是否存在内存泄露的问题了(当然也可以检测Fragment)
4)当发现有内存泄漏时,Leakcanary就会弹出一个通知栏消息告诉你哪里存在内存泄露的情况。
5)点击该通知栏消息,显示内存泄露详情;
可以看到LoginPhoneActivity中存在内存泄露的情况,继续往下看,可以定位到Config.currentContext这里,下面就是具体问题具体分析了;
(四)leakcanary的实现原理
在github的实现原理中有这样的一段话:
- RefWatcher.watch() creates a KeyedWeakReference to the watched object.
- Later, in a background thread, it checks if the reference has been cleared and if not it triggers a GC.
- If the reference is still not cleared, it then dumps the heap into a .hprof file stored on the app file system.
- HeapAnalyzerService is started in a separate process and HeapAnalyzer parses the heap dump using HAHA.
- HeapAnalyzer finds the KeyedWeakReference in the heap dump thanks to a unique reference key and locates the leaking reference.
- HeapAnalyzer computes the shortest strong reference path to the GC Roots to determine if there is a leak, and then builds the chain of references causing the leak.
- The result is passed back to DisplayLeakService in the app process, and the leak notification is shown.
英文水平不太好不在这里献丑了,不懂得童鞋可以自己理解一下;
另外对github项目,开源项目解析感兴趣的同学可以参考我的:
Github项目解析(一)–>上传android项目至github
Github项目解析(二)–>将Android项目发布至JCenter代码库
- Github项目解析(三)-->Android内存泄露监测之leakcanary
- Android内存泄露监测工具之leakcanary
- Android内存泄露监测之leakcanary
- 使用leakcanary检测Android项目内存泄露
- Android内存泄露实时监测——LeakCanary(Android Studio+Eclipse)
- Android内存泄露实时监测——LeakCanary/MAT等(Android Studio+Eclipse),测试
- LeakCanary(二)内存泄露监测原理研究
- Android 内存泄露神器之LeakCanary
- Android 内存泄露检测工具 LeakCanary
- Android内存泄露检测(LeakCanary)
- LeakCanary-Android内存泄露检测
- Android内存泄露自动检测LeakCanary
- android leakCanary检测内存泄露
- Android内存泄露自动检测LeakCanary
- Android LeakCanary 检测内存泄露
- Android 在 Eclipse 项目中使用 Leakcanary 内存泄露检测
- LeakCanary在检测Android项目的内存泄露
- 【Android】【内存】内存泄露检查工具 LeakCanary
- callgrind 使用
- ANSI C Yacc grammar
- 安卓开发linux内核驱动
- android anr trace.txt文件 抓取
- 新增软件测试视频资料
- Github项目解析(三)-->Android内存泄露监测之leakcanary
- redis与Mysql数据库的同步
- JAVA加密解密之对称加密
- 2016前端学习规划
- 欢迎使用CSDN-markdown编辑器
- php中strtr和str_replace的坑
- JS日期:根据出生日期 得到周岁年龄
- GridView的一些特殊属性(未完)
- PHP获取路径和目录方法总结