MyException - 我的异常网
当前位置:我的异常网» Android » Android Bit地图内存溢出有关问题解释

Android Bit地图内存溢出有关问题解释

www.MyException.Cn  网友分享于:2013-02-21  浏览:8次
Android Bitmap内存溢出问题解释


createBitmap好像有一个参数,可以绕过虚拟机的堆栈检查。内存报错其实是先检查是否超过限制,比如最大16M,你要分配32M,检查的时候超标,则会报错。除此之外,没有别的办法可以解决该问题。我们当时测试了30M的图片分配,如果不加该参数,则必然报错。加了就没事了。该参数是hidden的。你必须用源码编译才可以。而只有那个函数可以用。其余都用不了。该参数是BitmapFactory.options类的public boolean inNativeAlloc。不过4.0已经去掉该参数了。所以这个办法可能也不行。



BitmapFactory.Options options = new BitmapFactory.Options();
options.inNativeAlloc = true;


        * Normally bitmap allocations count against the dalvik heap, which 
     * means they help trigger GCs when a lot have been allocated. However, 
     * in rare cases, the caller may want to allocate the bitmap outside of 
        * that heap. To request that, set inNativeAlloc to true. In these 
        * rare instances, it is solely up to the caller to ensure that OOM is 
        * managed explicitly by calling bitmap.recycle() as soon as such a 
        * bitmap is no longer needed. 
     * @hide pending API council approval 

我在Android Developers 论坛上找到了一段话:
> On Wed, Jun 8, 2011 at 10:17 AM, Erik R <> wrote:
> > I'm working on a simple image manipulation app that requires opening
> > bitmaps at full resolution, which of course results in OutOfMemory
> > issues. I know that the short answer is to simply use less memory via
> > BitmapFactory's inSampleSize Option to downsample the bitmap, but for
> > this app I really would like to retain the full resolution for
> > editing. One solution I have been investigating is loading the bitmap
> > entirely on the native heap, after learning that the memory limitation
> > is only imposed within the Dalvik VM heap. A look into the Android
> > source code revealed that this is already the case... BitmapFactory
> > uses native methods to load a bitmap on the native heap, while
> > maintaining a reference to it on the VM heap. The issue then is that
> > it appears the native memory used by the bitmap is actually counted
> > against the available VM memory, when it really isn't there. A look
> > into the stock Camera and Gallery apps' source revealed that they get
> > around this by using an additional BitmapFactory Option,
> > inNativeAlloc. I was able to find this field in the Java code for
> > BitmapFactory:




软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有