Android内存泄露自动检测神器LeakCanary
经典的面试题:
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 点击打开链接
Android内存泄露自动检测神器LeakCanary的更多相关文章
- Android 内存泄露总结(附内存检测工具)
https://segmentfault.com/a/1190000006852540 主要是分三块: 静态储存区:编译时就分配好,在程序整个运行期间都存在.它主要存放静态数据和常量. 栈区:当方法执 ...
- Android内存泄露总结
内存泄露是如何产生的? 当一个对象已经不需要再使用了,本该被回收时,而有另外一个正在使用的对象持有它的引用从而导致它不能被回收,这导致本该被回收的对象不能被回收而停留在堆内存中,这就产生了内存泄漏. ...
- Android内存泄漏的检测流程、捕捉以及分析
https://blog.csdn.net/qq_20280683/article/details/77964208 Android内存泄漏的检测流程.捕捉以及分析 简述: 一个APP的性能,重度关乎 ...
- (转)专项:Android 内存泄露实践分析
今天看到一篇关于Android 内存泄露实践分析的文章,感觉不错,讲的还算详细,mark到这里. 原文发表于:Testerhome: 作者:ycwdaaaa ; 原文链接:https://teste ...
- CPP-基础:内存泄露及其检测工具
[转]浅谈C/C++内存泄露及其检测工具 对于一个c/c++程序员来说,内存泄漏是一个常见的也是令人头疼的问题.已经有许多技术被研究出来以应对这个问题,比如 Smart Pointer,Garba ...
- Android内存泄露分析之StrictMode
转载请注明地址:http://blog.csdn.NET/yincheng886337/article/details/50524709 StrictMode(严格模式)使用 StrictMode严格 ...
- iOS内存泄漏自动检测工具PLeakSniffer
新款objective-C内存泄漏自动检测工具 PLeakSniffer , GitHub地址 (https://github.com/music4kid/PLeakSniffer). 背景 前些天读 ...
- Android内存泄露---检测工具篇
内存使用是程序开发无法回避的一个问题.如果我们毫不在意肆意使用,总有一天会为此还账,且痛不欲生...所以应当防患于未然,把内存使用细化到平时的每一行代码中. 内存使用概念较大,本篇先讲对已有app如何 ...
- Android内存泄露
Android 内存泄漏是一个十分头疼的事情.LeakCanary是一款开源软件,主要作用是检测 Android APP 内存泄露.比起以前的 MAT 工具,LeakCanary 有着十分强大的功能, ...
随机推荐
- IOS笔记049-UITabBarController
1.简单实现 效果:在视图底部显示一个工具栏 代码实现 // 创建窗口 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScre ...
- IOS开发学习笔记021-练习2
只是简单练习一下,主要是学习里面的思想,处理问题的方法. 不过还有一个问题没想到解决方法. 那就是动态生成的按钮如何绑定按钮事件,请哪位大神指点一下啊.(知道怎么办了,原来是方法addTarget) ...
- Windows网络编程笔记4 -- Winsock 协议相关知识
Win32平台上的Winsock编程,Winsock是一个与协议无关的接口.以下协议是我们需要了解的: 网络协议的特征包括: 1. 面向消息 2. 面向连接和无线接 3. 可靠性和次序性 4. ...
- MongoDB快速入门学习笔记6 MongoDB的文档删除操作
db.集合名称.remove({query}, justOne)query:过滤条件,可选justOne:是否只删除查询到的第一条数据,值为true或者1时,只删除一条数据,默认为false,可选. ...
- BugKu-妹子的陌陌
打开后看这张图片,先放winhex里面,文件头FFD8,是jpg图片.看文件尾并不是FFD9,所以binwalk分析一下. 发现有一个rar文件,然后用foremost分离.发现里面有个加密的rar文 ...
- Python subprocess.Popen中communicate()和wait()区别
刚开始我是使用的wait(),但是当adb命令返回太多时,程序就会卡死,查询得知原因后,才使用了communicate(),communicate()返回一个元组:(stdoutdata, stder ...
- 【Python Selenium】简单数据生成脚本
最近因工作需要,写了一个简单的自动化脚本,纯属学习,顺便学习下selenium模块. 废话不多说,直接上代码!! 这里一位大神重写了元素定位.send_keys等方法,咱们直接进行调用. 适用Pyth ...
- 【bzoj3834】[Poi2014]Solar Panels 数论
题目描述 Having decided to invest in renewable energy, Byteasar started a solar panels factory. It appea ...
- BZOJ 3462 DZY Loves Math II ——动态规划 组合数
好题. 首先发现$p$是互质的数. 然后我们要求$\sum_{i=1}^{k} pi*xi=n$的方案数. 然后由于$p$不相同,可以而$S$比较小,都是$S$的质因数 可以考虑围绕$S$进行动态规划 ...
- Codeforces Round #440(Div.2)
一句话题意: A:给出两个长为\(n\),\(m\)的的数组,每个数在\(1\)到\(9\)之间,求出一个最小的数使得至少有一位出现在一个数组中,且至少有一位出现在另一个数组中.\(n,m\leq9\ ...