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代码库

4 0
原创粉丝点击