`

Android Out Of Memory(OOM) 的详细研究

阅读更多

基于Android开发多媒体和游戏应用时,可能会挺经常出现Out Of Memory 异常 ,顾名思义这个异常是说你的内存不够用或者耗尽了。
        在Android中,一个Process 只能使用16M内存,如果超过了这个限制就会跳出这个异常。这样就要求我们要时刻想着释放资源。Java的回收工作是交给GC的,如何让GC能及时的回收已经不是用的对象,这个里面有很多技巧,大家可以google一下。
        因为总内存的使用超过16M而导致OOM的情况,非常简单,我就不继续展开说。值得注意的是Bitmap在不用时,一定要recycle,不然OOM是非常容易出现的。
        本文想跟大家一起讨论的是另一种情况:明明还有很多内存,但是发生OOM了。
        这种情况经常出现在生成Bitmap的时候。有兴趣的可以试一下,在一个函数里生成一个13m 的int数组。
        再该函数结束后,按理说这个int数组应该已经被释放了,或者说可以释放,这个13M的空间应该可以空出来,
        这个时候如果你继续生成一个10M的int数组是没有问题的,反而生成一个4M的Bitmap就会跳出OOM。这个就奇怪了,为什么10M的int够空间,反而4M的Bitmap不够呢?
       这个问题困扰很久,在网上,国外各大论坛搜索了很久,一般关于OOM的解释和解决方法都是,如何让GC尽快回收的代码风格之类,并没有实际的支出上述情况的根源。
       直到昨天在一个老外的blog上终于看到了这方面的解释,我理解后归纳如下:
         在Android中:
       1.一个进程的内存可以由2个部分组成:java 使用内存 ,C 使用内存 ,这两个内存的和必须小于16M,不然就会出现大家熟悉的OOM,这个就是第一种OOM的情况。
       2.更加奇怪的是这个:一旦内存分配给Java后,以后这块内存即使释放后,也只能给Java的使用,这个估计跟java虚拟机里把内存分成好几块进行缓存的原因有关,反正C就别想用到这块的内存了,所以如果Java突然占用了一个大块内存,即使很快释放了:
        C能使用的内存 = 16M - Java某一瞬间占用的最大内存。
       而Bitmap的生成是通过malloc进行内存分配的,占用的是C的内存,这个也就说明了,上述的4MBitmap无法生成的原因,因为在13M被Java用过后,剩下C能用的只有3M了。
 
下面是我参考的blog的所有内容:
内如如下:

> You might try to pre-allocate bitmap memory before launching the WebViews?It's not the WebView that's triggering the OOM, but some arbitrary otherpiece of code that needs memory that is not *there* anymore. Very often thisis happening when starting a new activity.

Ok, I see, I have to start dealing with automating my apology. 

There is one more, small thing that I can do. I also do some downloading andXML parsing in the background at times. This only takes Java Heap (<3MB),but maybe I should move that stuff to a separate process. This may lower thechances of an OOM. I'll think about it, but with all the added complexity ofinter process communication I am not sure I would want to go there.

Anyway, thanks for sharing your insights. That was very helpful.

 On Wed, Oct 7, 2009 at 10:48 PM, Tom Gibara <[EMAIL PROTECTED]> wrote: 
> I think it's better to add a couple more columns to the table to see the 
> picture (as I see it) more clearly:> JH = Java Heap
> JU = Memory actually used by Java
> NH = Native Heap 
> TU = Total memory Used = JU + NH
> TA = Total memory Allocated = JH + NH 
>
> (note I'm not distinguishing between native heap and native heap used 
> because it's not relevant here)
>
> The system requires TA (the process size as you called it) to not exceed 
> 16MB, the evolution is:
> JU JH NH TU TA 
> 1) 2 2 0 2 2
> 2) 4 4 0 4 4
> 3) 4 4 2 6 6 
> 4) 14 14 2 16 16
> 5) 4 14 2 6 16 
> 6) 4 14 4 10 18 *** OH NO! *** 
>
> The key is what happens between (4) and (5): GC reclaims 10MB (JU reduced> by 10MB) but the java heap doesn't shrink (JH stays at 14MB). This enlarged > java heap basically squeezes the maximum native heap allocation.
> The simplest approach is to try and ensure that your application maintains 
> a 'flatish' memory profile - no big spikes. You should do this anyway, since 
> it means that your application is being well behaved and won't force other 
> apps to be terminated just because your application needs a temporary shot> of memory (which will then remain as a glut until the application restarts).
>
> As you point out, WebViews are heavy on memory usage, and these might be 
> what's causing your memory usage to spike. I don't have any good suggestions 
> for a fix. You might try to pre-allocate bitmap memory before launching the 
> WebViews? It might work, but it may be complicated to do and could cause 
> OOMs when WebViews are instantiated - no way around that, your application
> is simply using too much memory at that point. 
>

分享到:
评论

相关推荐

    ANDROIDBITMAP内存限制OOM,OUTOFMEMORY.pdf

    ANDROIDBITMAP内存限制OOM,OUTOFMEMORY.pdf

    ANDROIDBITMAP内存限制OOM,OUTOFMEMORY[文].pdf

    ANDROIDBITMAP内存限制OOM,OUTOFMEMORY[文].pdf

    Android-OOM.rar_memory android_memory for Android_out

    关于Android out of memory错误的分析

    android camera out of memory安卓照相机OOM问题的解决

    如果不能使用,请修改根目录下的project.property的android:target为你当前有的target(不知道怎么改的同学可以从8到21一个个数字去试哦) 程序实现点击屏幕后聚焦拍照功能,并把图片存入sd卡camera目录下。但打开时无...

    Android 加载大图及多图避免程序出现OOM(OutOfMemory)异常

    主要介绍了Android 加载大图及多图避免程序出现OOM(OutOfMemory)异常的相关资料,需要的朋友可以参考下

    解决Android解析图片的OOM问题的方法!!!

    我们在编写Android程序的时候经常要用到许多图片,不同图片总是会有不同的形状、不同的大小,程序占用了过高的内存就容易出现OOM(OutOfMemory)异常。本篇文章主要讲诉了解决Android解析图片的OOM问题,有兴趣的可以...

    解决Android平台中应用程序OOM异常的方法

    首先,OOM就是内存溢出,即Out Of Memory。也就是说内存占有量超过了VM所分配的最大。 怎么解决OOM,通常OOM都发生在需要用到大量内存的情况下(创建或解析Bitmap,分配特大的数组等),在这样的一种情况下,就可能...

    android图片缓存优化,内存缓存加sdcard缓存,性能很好,防止oom

    一个图片内存缓存和sdcard缓存的例子,经过很好的优化,性能很好,能有效的防止oom.

    【性能】OOM原理解析:LowMemoryKiller原理

    1 概述 Android的设计理念之一,便是应用程序退出,但进程还会继续存在系统以便...Android基于Linux的系统,其实Linux有类似的内存管理策略——OOM killer,全称(Out Of Memory Killer), OOM的策略更多的是用于分配内存

    Android利用软引用和弱引用避免OOM的方法

    想必很多朋友对OOM(OutOfMemory)这个错误不会陌生,而当遇到这种错误如何有效地解决这个问题呢?今天我们就来说一下如何利用软引用和弱引用来有效地解决程序中出现的OOM问题. 一.了解 强引用、软引用、弱引用、虚...

    Android内存回收机制

    Android内存回收机制策略 1、GC 2、lowmemorykiller GC GC是java虚拟机的内存...OOM(Out Of Memory) : Android内存管理机制及优化方法(https://www.2cto.com/kf/201805/741791.html) 简言之,即应用占用的最大内存

    Android获取本地相册图片和拍照获取图片的实现方法

    需求:从本地相册找图片,或通过调用系统相机拍照得到图片。 容易出错的地方: 1、当我们指定了照片的uri路径,我们就不能通过data.getData();...就容易出现 out of memory(oom)错误,我们需要先把URI转换

    Android 内存泄漏的几种可能总结

    如果不小心,你的Android应用很容易浪费掉未释放的内存,最终导致内存用光的错误抛出(out-of-memory,OOM)。 一般内存泄漏(traditional memory leak)的原因是:当该对象的所有引用都已经释放了,对象仍未被释放。...

    Android图片缓存之Lru算法(二)

    我们得知一个应用如果使用大量图片就会导致OOM(out of memory),那该如何处理才能近可能的降低oom发生的概率呢?之前我们一直在使用SoftReference软引用,SoftReference是一种现在已经不再推荐使用的方式,因为从 ...

    Android 加载大图、多图和LruCache缓存详细介绍

    大家应该知道,我们编写的应用程序都是有一定内存限制的,程序占用了过高的内存就容易出现OOM(OutOfMemory)异常。我们可以通过下面的代码看出每个应用程序最高可用内存是多少 int maxMemory = (int) (Runtime....

    Android 内存溢出和内存泄漏的问题

    内存溢出 (OOM)是指程序在申请内存时,没有足够的内存空间供其使用,出现out of memory;比如只申请了一个integer,但给它存了long才能存下的数,那就会出现内存溢出。 内存泄露 (memory leak)是指程序在申请内存...

    android BitmapFactory.Options使用方法详解

    方法将突破转成Bitmap时,遇到大一些的图片,我们经常会遇到OOM(Out Of Memory)的问题。怎么避免它呢? 这就用到了我们上面提到的BitmapFactory.Options这个类。 BitmapFactory.Options这个类,有一个字

    安卓开发性能优化总结

    Android的虚拟机是基于寄存器的Dalvik,它的最大堆大小一般是16M,有的机器为24M。...如果我们的内存占用超过了一定的水平就会出现OutOfMemory的错误。 掌握OOM异常的处理,并可以对应用进行相应的优化

    Android开发中Bitmap高效加载使用详解

    在Android开发中,我们经常与Bitmap打交道,而对Bitmap的不恰当的操作经常会导致OOM(Out of Memory)。这篇文章我们会介绍如何高效地在Android开发中使用Bitmap,在保证图片显示质量的前提下尽可能占用更小的内存。

Global site tag (gtag.js) - Google Analytics