之前碰到的OOM问题,终于很直白的呈现在我的眼前:我尝试了MAT,但是发现不怎么会用。直到今天终于发现了这个新工具:

当我们的App中存在内存泄露时会在通知栏弹出通知:

当点击该通知时,会跳转到具体的页面,展示出Leak的引用路径,如下图所示:

LeakCanary 可以用更加直白的方式将内存泄露展现在我们的面前。

以下是我找到的学习资料,写的非常棒:
1、LeakCanary: 让内存泄露无所遁形
2、LeakCanary 中文使用说明

AndroidStudio (官方)上使用LeakCanary 请移步:
https://github.com/square/leakcanary

Eclipse 上使用LeakCanary 请移步我的:
https://github.com/SOFTPOWER1991/LeakcanarySample-Eclipse

Android studio (自己弄的)上使用LeakCanary也可以看这个:

leakcanarySample_androidStudio

工程包括:

  1. LeakCanary库代码
  2. LeakCanaryDemo示例代码

使用步骤:

  1. 将LeakCanary import 入自己的工程

  2. 添加依赖:

    compile project(':leakcanary')

  3. 在Application中进行配置

    public class ExampleApplication extends Application {
    
      ......
    //在自己的Application中添加如下代码
    public static RefWatcher getRefWatcher(Context context) {
    ExampleApplication application = (ExampleApplication) context
    .getApplicationContext();
    return application.refWatcher;
    } //在自己的Application中添加如下代码
    private RefWatcher refWatcher; @Override
    public void onCreate() {
    super.onCreate();
    ......
    //在自己的Application中添加如下代码
    refWatcher = LeakCanary.install(this);
    ......
    } .....
    }
  4. 在Activity中进行配置

public class MainActivity extends AppCompatActivity {

    ......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//在自己的应用初始Activity中加入如下两行代码
RefWatcher refWatcher = ExampleApplication.getRefWatcher(this);
refWatcher.watch(this); textView = (TextView) findViewById(R.id.tv);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startAsyncTask();
}
}); } private void async() { startAsyncTask();
} private void startAsyncTask() {
// This async task is an anonymous class and therefore has a hidden reference to the outer
// class MainActivity. If the activity gets destroyed before the task finishes (e.g. rotation),
// the activity instance will leak.
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
// Do some slow work in background
SystemClock.sleep(20000);
return null;
}
}.execute();
} }
  1. 在AndroidMainfest.xml 中进行配置,添加如下代码
        <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>

5、测试结果

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

b、通知显示

c、桌面自动添加的图表

d、内存泄露列表

e、内存泄露详细

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

  1. 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".
  2. 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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.micky.leakcanarysamples"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="10"
  8. android:targetSdkVersion="23" />
  9. <!-- To store the heap dumps and leak analysis results. -->
  10. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  11. <android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  12. <application
  13. android:name="com.micky.leakcanarysamples.BaseApplication"
  14. android:allowBackup="true"
  15. android:icon="@mipmap/ic_launcher"
  16. android:label="@string/app_name"
  17. android:supportsRtl="true"
  18. android:theme="@style/AppTheme" >
  19. <activity
  20. android:name="com.micky.leakcanarysamples.MainActivity"
  21. android:label="@string/app_name"
  22. android:theme="@style/AppTheme.NoActionBar" >
  23. <intent-filter>
  24. <action android:name="android.intent.action.MAIN" />
  25. <category android:name="android.intent.category.LAUNCHER" />
  26. </intent-filter>
  27. </activity>
  28. <activity android:name="com.micky.leakcanarysamples.TestActivity" />
  29. <service
  30. android:name="com.squareup.leakcanary.internal.HeapAnalyzerService"
  31. android:enabled="false"
  32. android:process=":leakcanary" />
  33. <service
  34. android:name="com.squareup.leakcanary.DisplayLeakService"
  35. android:enabled="false" />
  36. <activity
  37. android:name="com.squareup.leakcanary.internal.DisplayLeakActivity"
  38. android:enabled="false"
  39. android:icon="@drawable/__leak_canary_icon"
  40. android:label="@string/__leak_canary_display_activity_label"
  41. android:taskAffinity="com.squareup.leakcanary"
  42. android:theme="@style/__LeakCanary.Base" >
  43. <intent-filter>
  44. <action android:name="android.intent.action.MAIN" />
  45. <category android:name="android.intent.category.LAUNCHER" />
  46. </intent-filter>
  47. </activity>
  48. </application>
  49. </manifest>

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



It 's so simple.

注:

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

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

Android内存优化8 内存检测工具2 LeakCanary——直白的展现Android中的内存泄露的更多相关文章

  1. LeakCanary——直白的展现Android中的内存泄露

    之前碰到的OOM问题,终于很直白的呈现在我的眼前:我尝试了MAT,但是发现不怎么会用.直到今天终于发现了这个新工具: 当我们的App中存在内存泄露时会在通知栏弹出通知: 当点击该通知时,会跳转到具体的 ...

  2. 56、LeakCanary——直白的展现Android中的内存泄露

    转载:http://blog.csdn.net/watermusicyes/article/details/46333925 DEMO下载地址:https://github.com/SOFTPOWER ...

  3. Linux内存都去哪了:(1)分析memblock在启动过程中对内存的影响

    关键词:memblock.totalram_pages.meminfo.MemTotal.CMA等. 最近在做低成本方案,需要研究一整块RAM都用在哪里了? 最直观的的就是通过/proc/meminf ...

  4. Android 性能优化 五 性能分析工具dumpsys的使用

    Android提供的dumpsys工具能够用于查看感兴趣的系统服务信息与状态,手机连接电脑后能够直接命令行运行adb shell dumpsys 查看全部支持的Service可是这样输出的太多,能够通 ...

  5. Android应用优化之代码检测优化

    在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...

  6. Android内存泄漏检测利器:LeakCanary

    Android内存泄漏检测利器:LeakCanary MAR 28TH, 2016 是什么? 一言以蔽之:LeakCanary是一个傻瓜化并且可视化的内存泄露分析工具 为什么需要LeakCanary? ...

  7. 【腾讯Bugly干货分享】Android内存优化总结&实践

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/2MsEAR9pQfMr1Sfs7cPdWQ 导语 智 ...

  8. Android性能优化-内存优化

    原文链接 Manage Your App’s Memory 前言 在任何软件开发环境中,RAM都是比较珍贵的资源.在移动操作系统上更是这样,因为它们的物理内存通常受限.尽管在ART和Dalvik虚拟机 ...

  9. 大礼包!ANDROID内存优化(大汇总)

    写在最前: 本文的思路主要借鉴了2014年AnDevCon开发者大会的一个演讲PPT,加上把网上搜集的各种内存零散知识点进行汇总.挑选.简化后整理而成. 所以我将本文定义为一个工具类的文章,如果你在A ...

随机推荐

  1. [bugfix]copy属性参数将NSMutableArray变为NSArray类型

    问题:NSMutableArray 声明为 copy 属性参数后即使接受NSMutableArray变量依然为NSArray变量 测试: 属性申明为: 1 @property (nonatomic, ...

  2. python初学-元组、集合

    元组: 元组基本和列表一样,区别是 元组的值一旦创建 就不能改变了 tup1=(1,2,3,4,5) print(tup1[2]) ---------------------------------- ...

  3. gradle eclipse 配置

    http://blog.csdn.net/caolaosanahnu/article/details/17022321 从gradle官网下载 解压,配置环境变量,gradle -v 验证 gradl ...

  4. .vue,跟小程序文件在sublime里面怎么实现代码格式化

    .vue文件跟小程序的.wxml,.wxss用sublime的HTML/CSS/JS prettify插件也可以实现格式化代码的效果 首先你在sublime要已经安装好了HTML/CSS/JS pre ...

  5. 三:基于Storm的实时处理大数据的平台架构设计

    一:元数据管理器==>元数据管理器是系统平台的“大脑”,在任务调度中有着重要的作用[1]什么是元数据?--->中介数据,用于描述数据属性的数据.--->具体类型:描述数据结构,数据的 ...

  6. (编译)使用 AppCenter 持续输出导出到 Application Insights

    原文地址:https://blog.xamarin.com/appcenter-continuous-export-application-insights/ 五星手机应用有一个特殊的特点:他们不会放 ...

  7. Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---工厂模式之简单工厂

    简单工厂:工厂依据传进的参数创建相应的产品. http://www.cnblogs.com/DelphiDesignPatterns/archive/2009/07/24/1530536.html { ...

  8. Nodejs项目重复文件扫描

    项目地址:https://github.com/danielstjules/jsinspect 1.安装jsinspect npm install -g jsinspect 2.进入至项目目录 d c ...

  9. 二分查找(BinarySearch)

    http://blog.csdn.net/magicharvey/article/details/10282801 简单描述 二分查找,又名折半查找,是一种在有序序列中查找特定元素的搜索算法.搜素过程 ...

  10. sed 插入和替换

    sed -i '/参考行/i\插入内容' *.ksh sed -i 's,原内容,替换后内容,g' *.ksh