您的位置:首页 > 移动开发 > Android开发

Android内存泄露自动检测神器LeakCanary

2016-01-04 11:30 369 查看
经典的面试题:

a、怎样在coding过程中避免内存泄露?

b、怎样检测内存泄露?

这两个问题我想大部分android 职位面试时都会被问到吧。

怎样避免就不赘述了,网上很多答案。

工具呢,当然也有很多,比如DDMS、MAT等,但是怎样在我们编码过程中植入内存检测代码,让我们程序在开发调试阶段就能发现内存泄露呢?好了,现在该大名鼎鼎的LeakCanary出场了,它是Square公司的一个内存探测开源项目。下面就介绍下怎样使用.

1、配置gradle依赖:

debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'


2、初始化Watcher

package com.micky.leakcanarysamples;;

import android.app.Application;

import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;

/**
* @Project LeakCanaryTest
* @Packate com.micky.leakcanarysamples;
* @Description
* @Author Micky Liu
* @Email mickyliu@126.com
* @Date 2016-01-04 10:32
* @Version 1.0
*/
public class BaseApplication extends Application {

private static BaseApplication instance;

private RefWatcher mRefWatcher;

@Override
public void onCreate() {
super.onCreate();
instance = this;
mRefWatcher = Constants.DEBUG ?  LeakCanary.install(this) : RefWatcher.DISABLED;
}

public static BaseApplication getInstance() {
return instance;
}

public static RefWatcher getRefWatcher() {
return getInstance().mRefWatcher;
}
}


3、在Activity或Fragment中添加检测

package com.micky.leakcanarysamples;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;

/**
* @Project LeakCanaryTest
* @Packate com.micky.leakcanarysamples;
* @Description
* @Author Micky Liu
* @Email mickyliu@126.com
* @Date 2016-01-04 10:39
* @Version 1.0
*/
public class BaseActivity extends AppCompatActivity {

@Override
protected void onDestroy() {
super.onDestroy();
BaseApplication.getRefWatcher().watch(this);
}
}


4、测试
package com.micky.leakcanarysamples;;

import android.os.Bundle;
import android.os.Handler;

/**
* @Project LeakCanaryTest
* @Packate com.micky.leakcanarysamples;
* @Description
* @Author Micky Liu
* @Email mickyliu@126.com
* @Date 2016-01-04 10:29
* @Version 1.0
*/
public class TestActivity extends BaseActivity {

private final Handler mHandler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//模拟内存泄露
mHandler.postDelayed(new Runnable() {
@Override
public void run() {

}
}, 3 * 60 * 1000);
finish();
}
}


5、测试结果

a、Toast显示(大概10秒左右显示)



b、通知显示



c、桌面自动添加的图表



d、内存泄露列表



e、内存泄露详细



LogCat可以看到日志日下(hprof文件可以用MAT打开进行分析):

01-04 11:49:41.815 12967-13004/com.micky.leakcanarysamples I/dalvikvm: hprof: dumping heap strings to "/storage/emulated/0/Download/leakcanary/suspected_leak_heapdump.hprof".
01-04 11:49:42.020 12967-13004/com.micky.leakcanarysamples I/dalvikvm: hprof: heap dump completed (28850KB)


查看自动生成的AndroidManifest文件,LeakCanarySamples/app/build/intermediates/manifests/full/debug/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.micky.leakcanarysamples"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="23" />

<!-- To store the heap dumps and leak analysis results. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:name="com.micky.leakcanarysamples.BaseApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name="com.micky.leakcanarysamples.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.micky.leakcanarysamples.TestActivity" />

<service
android:name="com.squareup.leakcanary.internal.HeapAnalyzerService"
android:enabled="false"
android:process=":leakcanary" />
<service
android:name="com.squareup.leakcanary.DisplayLeakService"
android:enabled="false" />

<activity
android:name="com.squareup.leakcanary.internal.DisplayLeakActivity"
android:enabled="false"
android:icon="@drawable/__leak_canary_icon"
android:label="@string/__leak_canary_display_activity_label"
android:taskAffinity="com.squareup.leakcanary"
android:theme="@style/__LeakCanary.Base" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


如上所示LeakCanary给我们自动添加了两个Service和一个Activity,并添加了对SD卡的读写权限

It 's so simple.
注:

1、如果在Release模式下请使用RefWatcher.DISABLED

2、在Activity或Fragment 的 Destroy方法中添加检测(很好理解,就是判断一个Activity或Fragment想要被销毁的时候,是否还有其他对象持有其引用导致Activity或Fragment不能被回收,从而导致内存泄露)

源码地址:https://github.com/mickyliu945/LeakCanarySample 点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: