Android OOM产生原因及如何解决

转载出处:Lee http://blog.csdn.net/hnulwt/article/details/44900761

OOM产生原因

OOM产生可能的原因是因为 1、加载大图片导致内存溢出 2、大量内存泄露 
OOM产生的本质是什么呢? 
Dalvik VM主要管理的内存 Java heap,由于手机设备的限制,一般一个应用使用的内存不能超过默认值 32M(不同设备略有差异,通过adb shell getprop | grep dalvik.vm.heapgrowthlimit命令查看),这也就是说,当在DVM上申请的堆内存大于默认阀值的时候,我们的应用就会抛出OutOfMemoryError。

如何解决和避免OOM

1、解决大图片导致内存溢出

加载多图:

(1)使用软引用、弱引用,当堆内存不足的时候,就可以自动的释放这些缓存的Bitmap对象。

这里有一篇:关于Bitmap分配在native heap还是dalvik heap上的说明。 
关于软引用的说明: 
软引用(SoftReference)是用来设计object-cache的。他在JVM报告内存不足之前会清除所有的软引用,这样以来gc就有可能收集软可及的对象,可能解决内存不足的问题,避免内存溢出。什么时候会被收集取决于gc的算法和gc运行时可用内存的大小。

关于弱应用的说明:看一个例子更容易懂:

<code class="hljs vhdl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-typename" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span> test =<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-typename" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"aaa"</span>);       
WeakReference<<span class="hljs-typename" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span>> testWeak = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> WeakReference<<span class="hljs-typename" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span>>(test);       
test = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>;       
System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"before: "</span>+ testWeak.get());       
System.gc();       
System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"after: "</span>+ testWeak.get()); </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

结果: 
before: aaa 
after: null 
如果你希望能随时取得某对象的信息,但又不想影响此对象的垃圾收集,那么你应该用 弱应用(Weak Reference)来记住此对象。

(2)使用过的图并且不再使用,可以调用Bitmap.recycle()加速回收。
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span> != bitmap && !bitmap.isRecycled()) {  
    bitmap.recycle();  
}  </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
(3)考虑使用文件缓存。

整个大图都需要加载:

得到bitmap之前先利用BitmapFactory.Options的inSampleSize的值得到压缩图片。 
关键代码:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    // 第一次解析将inJustDecodeBounds设置为true,来获取图片大小 
    final BitmapFactory<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Options</span> options = new BitmapFactory<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Options</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; </span>
    options<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.inJustDecodeBounds</span> = true<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; </span>
    BitmapFactory<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.decodeResource</span>(res, resId, options)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; </span>
    // 调用上面定义的方法计算inSampleSize值, calculateInSampleSize方法自己写,这里不再赘述
    options<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.inSampleSize</span> = calculateInSampleSize(options<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.outWidth</span>, options<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.outHeight</span>, reqWidth, reqHeight)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

    options<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.inJustDecodeBounds</span> = false<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; </span>
    Bitmap bmp = BitmapFactory<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.decodeResource</span>(res, resId, options)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; </span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

只加载部分图片

可以考虑在API 10以后引进的BitmapRegionDecoder类,具体使用方式还未研究,源码注释(BitmapRegionDecoder is particularly useful when an original image is large and you only need parts of the image)。

2、解决内存泄露问题

解决该问题主要需要对Android系统各部分组件进行一些较深入了解,比如: 
对Activity的生命周期进行了解以后,就应该避免对生命周期之外的引用。一个应用可能有多个Activity构成,这时候应该考虑使用Application类。(该问题主要是针对Activity中静态对象的控制) 
尽量不要由于各种复杂的引用导致GC不能及时的甚至永远不能回收某块内存。

以上是我对OOM问题的一些解决方案,如果大家还有其他很好的方式恳请提出来,共同提升。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值