超赞的OOM检测(除了mat以外)
今天看了下微博,扔物线分享了个内存检测的工具:


LeakCanary: Detect all memory leaks!
java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCreate(Bitmap.java:-2)
at android.graphics.Bitmap.createBitmap(Bitmap.java:689)
at com.squareup.ui.SignView.createSignatureBitmap(SignView.java:121)
没有任何人会喜欢OOM问题的出现
In Square Register, we draw the customer's signature on a bitmap cache. This bitmap is the size of the device's screen, and we had a significant number of out of memory (OOM) crashes when creating it.
这个图片的大小是设备大小,我们在显示这个图片的时候会出现OOM的问题。

We tried a few approaches, none of which solved the issue:
我们尝试了一些方法,其中没有解决的问题:
- Use
Bitmap.Config.ALPHA_8(a signature doesn't need color). - 使用bitmap.config.alpha_8
- Catch
OutOfMemoryError, trigger the GC and retry a few times (inspired from GCUtils). - 抓住OutOfMemoryError,触发GC和重试几次(灵感来自gcutils)。
- We didn't think of allocating bitmaps off the Java heap. Lucky for us, Fresco didn't exist yet.
- 我们不想把Java堆分配位图。幸运的是,Fresco 这个开源项目不存在这种问题。
We were looking at it the wrong way
The bitmap size was not a problem. When the memory is almost full, an OOM can happen anywhere. It tends to happen more often in places where you create big objects, like bitmaps. The OOM is a symptom of a deeper problem: memory leaks.
位图是没问题的,如果内存满的话,一个oom会出现在任何地方。当你创建比较大的对象,像bitmap这种东西的地方就会出现这种问题,oom更深层次的问题就是内存泄漏的问题。
What is a memory leak?
Some objects have a limited lifetime. When their job is done, they are expected to be garbage collected. If a chain of references holds an object in memory after the end of its expected lifetime, this creates a memory leak. When these leaks accumulate, the app runs out of memory.
许多对象是有有限制的生命周期的,当他们工作完成后,就会被垃圾回收。当对象的引用在对象的生命周期后还在持有该引用的话,就会出现内存泄漏的问题。当引用累计到一定一定时候,就会出现OOM的问题。
For instance, after Activity.onDestroy() is called, the activity, its view hierarchy and their associated bitmaps should all be garbage collectable. If a thread running in the background holds a reference to the activity, then the corresponding memory cannot be reclaimed. This eventually leads to an OutOfMemoryError crash.
例如,在Actiivity ondestroy()后,它的视图层次结构及其相关的位图应该都是垃圾收集。如果一个线程在后台运行,持有该activity引用,那么相应的内存不能被回收。这最终导致OutOfMemoryError错误,崩溃。
捕获OOM的问题
Hunting memory leaks is a manual process, well described in Raizlabs’ Wrangling Dalvik series.
捕获OOM的问题是一个手动的过程。
Here are the key steps:
关键步骤如下:
- Learn about
OutOfMemoryErrorcrashes via Bugsnag, Crashlytics, or the Developer Console. 在使用Bugsnag, Crashlytics以及 Developer Console工具去检测OutOfMemoryError这种问题。- Attempt to reproduce the problem. You might need to buy, borrow, or steal the specific device that suffered the crash. (Not all devices will exhibit all leaks!) You also need to figure out what navigation sequence triggers the leak, possibly by brute force.
- 重现问题,你可能需要购买或者借到一些特殊的设备来重现crash问题(并不是所有的设备都会出现这种问题),你需要知道是什么导致内存泄漏的问题,有可能是蛮力(这句翻译的有点怪,我擦)。
- Dump the heap when the OOM occurs (here's how).
- 堆积OOM的问题
- Poke around the heap dump with MAT or YourKit and find an object that should have been garbage collected.
- 使用MAT或者 YourKit 工具去发现那些对象应该被回收。
- Compute the shortest strong reference path from that object to the GC roots.
- 计算最短路径的强引用对象的GC根。
- Figure out which reference in the path should not exist, and fix the memory leak.
- 找出那些引用是不应该存在的,并处理内存对这些引用。
What if a library could do all this before you even get to an OOM, and let you focus on fixing the memory leak?
如果有一个库帮助你在oom问题出现之前去检测OOM的问题,你会去关注如何修复内存引用的问题吗?
介绍使用LeakCanary
LeakCanary is an Open Source Java library to detect memory leaks in your debug builds.
LeakCanary是一个开源项目,通过java库去检测内存泄漏的问题。
Let's look at a cat example:
看下下面的例子:
class Cat {
}
class Box {
Cat hiddenCat;
}
class Docker {
static Box container;
}
// ...
Box box = new Box();
Cat schrodingerCat = new Cat();
box.hiddenCat = schrodingerCat;
Docker.container = box;
You create a RefWatcher instance and give it an object to watch:
你创造了一个refwatcher实例,给它一个对象去检测:
// We expect schrodingerCat to be gone soon (or not), let's watch it.
refWatcher.watch(schrodingerCat);
When the leak is detected, you automatically get a nice leak trace:
当内存检测的时候,你会检测到内存的内存的使用的痕迹。
* GC ROOT static Docker.container
* references Box.hiddenCat
* leaks Cat instance
We know you're busy writing features, so we made it very easy to setup. With just one line of code, LeakCanary will automatically detect activity leaks:
public class ExampleApplication extends Application {
@Override public void onCreate() {
super.onCreate();
LeakCanary.install(this);
}
}
You get a notification and a nice display out of the box:
你能得到通知,并且能够良好的展示在界面上:

Conclusion
After enabling LeakCanary, we discovered and fixed many memory leaks in our app. We even found a few leaks in the Android SDK.
在启用的leakcanary,我们能够发现并修复你APP里面出现的内存泄漏的问题。我们甚至发现了一些 leaks in the Android SDK。
The results are amazing. We now have 94% fewer crashes from OOM errors.
结果让人吃惊,能够检测出94%的OOM的问题。

If you want to eliminate OOM crashes, install LeakCanary now!
超赞的OOM检测(除了mat以外)的更多相关文章
- Pace.js – 超赞的页面加载进度自动指示和 Ajax 导航效果
在页面中引入 Pace.js 和您所选择主题的 CSS 文件,就可以让你的页面拥有漂亮的加载进度和 Ajax 导航效果.不需要挂接到任何代码,自动检测进展.您可以选择颜色和多种效果,有简约,闪光灯, ...
- 海量的超赞 Linux 软件 (转载)
海量的超赞 Linux 软件 作者: VoLuong 译者: LCTT Mo | 2016-08-24 16:01 评论: 27 收藏: 38 这个仓库收集了对任何用户/开发者都超赞的 Linux ...
- 分享29个超赞的响应式Web设计
原文自:http://www.csdn.net/article/2013-01-16/2813678-responsive-design-websites 最近几年,响应式Web设计不断印入人们眼帘, ...
- 超赞的CSS3进度条 可以随进度显示不同颜色
原文:超赞的CSS3进度条 可以随进度显示不同颜色 现在的WEB已经不是以前的WEB了,传输更大的数据量,有着更加复杂的计算,这就需要利用进度条来提高用户体验,必要时可以让用户耐心等待,不至于因操作卡 ...
- aos.js超赞页面滚动元素动画jQuery动画库
插件描述:aos.js 是一款效果超赞的页面滚动元素动画jQuery动画库插件.该动画库可以在页面滚动时提供28种不同的元素动画效果,以及多种easing效果.在页面往回滚动时,元素会恢复到原来的状态 ...
- 不容错过的超赞项目管理PPT
不容错过的超赞项目管理PPT(转载) 大公司的一个好处,是各个领域都有牛人,可以为你提供经验分享交流.腾讯庞大的培训体系更是保证了:如果你想学点什么东西,你总可以学到.腾讯内部资源30页PPT曝光 — ...
- DEMO大全,超赞【申明:来源于网络】
DEMO大全,超赞[申明:来源于网络] 地址:http://www.verydemo.com/one_c55.html
- 10个超赞的jQuery图片滑块动画
在网站开发过程中,特别是前端开发这块,经常会使用到很多图片滑块动画,也就是一些基于jQuery和HTML5的焦点图插件.本文将为大家收集10个超赞的jQuery图片滑块动画,这些现成的jQuery插件 ...
- 基于jQuery8款超赞的评分插件
基于jquery8款超赞的评分插件.这是一款基于jquery.barrating插件实现的,该评级小部件可灵活设置CSS样式.具体效果请查看演示.效果图如下: 在线预览 源码下载 实现的代码. h ...
随机推荐
- 为 ngui TweenPosition 添加 pingpongone
//---------------------------------------------- // NGUI: Next-Gen UI kit // Copyright © 2011-2015 T ...
- linux下安装tomcat,并设置自动启动
在linux系统下,设置某个服务自启动的话,需要在/etc/rcX.d下挂载,还要在/etc/init.d/下写启动脚本的 在/etc/init.d/下新建一个文件tomcat(需要在root权限下操 ...
- python--day4--迭代器、生成器
列表生成式: 需求:列表[1,2,3,4,5,6,7,8,9]每个值加1,实现的方法: a = [0,1,2,3,4,5,6,7,8,9] b = [] for i in a:b.append(i+1 ...
- STL容器小结
1.空间分配器 std::alloc用于容器中内存空间的分配和释放,以及分配内存的管理.construct().destroy()等全局函数用于为对象的构造和析构. 2.迭代器和trains 迭代器 ...
- power oj 1557种树[二进制状压DP]
题目链接[https://www.oj.swust.edu.cn/problem/show/1557] 题意:中文题目. 题解:用0,1表示某个位置是否种了树,先算出同一行的有效状态的总数,即开两个1 ...
- 五、oracle基本建表语句
--创建用户create user han identified by han default tablespaceusers Temporary TABLESPACE Temp;grant conn ...
- clone()方法、深复制和浅复制
clone方法 Java中没有明确提供指针的概念和用法,而实质上没个new语句返回的都是一个指针的引用,只不过在大部分情况下开发人员不需要关心如何去操作这个指针而已. 在实际编程中,经常会遇到从某个已 ...
- 给图片使用border-radius 图片会变成圆的。
- ACdream 1726 A Math game
深搜.不过有一个强大的剪枝.就是假设之后的全部用上都不能达到H,则return. if (A[n]-A[x-1]+summ< H) return; //A[n]表示前nx项和 #include& ...
- HDU 2544 最短路(初涉SPFA算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t ...