位图分配过程中的Andr​​oid 2.x时的Dalvik可能的错误? [英] Possible bug in dalvik of Android 2.x during Bitmap allocation?

查看:159
本文介绍了位图分配过程中的Andr​​oid 2.x时的Dalvik可能的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的现象:一是做分配在Java方面的一些大的内存块,直到我们catche的OutOfMemoryError,然后点击释放他们所有即可。现在,奇怪的事情发生了:负载即使是很小的画面(如宽度:200,高度:200)由BitmapFactory.de codeXXX(德codeResource,德codeFILE,...)将抛出一个OutOfMemoryError !但其确定的Alloc任何纯Java对象大(例如,新的字节[2 * 1024 * 1024])现在!

The phenomenon: First do allocation some big memory blocks in the Java side until we catche OutOfMemoryError, then free them all. Now, weird things happen: load even a small picture(e.g. width:200, height:200) by BitmapFactory.decodeXXX(decodeResource, decodeFile, ...) will throw an OutOfMemoryError! But its OK to alloc any pure Java big Object(e.g. new byte[2*1024*1024]) now!

验证:我写了一些简单的codeS验证可以下载的这里,preSS黄金按钮多少次,你会得到一个错误OOF,那么preSS自由所有,如今环境建立。现在,您可以preSSLoadBitmap,你会看到它不会在大多数Android 2.x的手机的工作。(但是在模拟器它只是OK,奇)

Verifying: I wrote some simple codes to verify the problem that can download here, press "Alloc" button many times and you will got an OOF Error, then press "Free All", now the environment is set up. Now you can press "LoadBitmap" and you will see its not work on most of Android 2.x phone.(But in the emulator its just OK, odd)

进一步挖掘:我尝试深入一些的Dalvik code,找出原因,并找到函数externalAllocPossible一个可能的错误中的 HeapSource.c 它通过dvmTrackExternalAllocation叫谁打印的xxx字节的外部分配太大,这一进程,在LogCat中的消息。

Digging deeper: I try to dig into some dalvik code to find out why, and find a possible bug in function externalAllocPossible in HeapSource.c which called by dvmTrackExternalAllocation who print the "xxx-byte external allocation too large for this process" messages in LogCat.

在externalAllocPossible简单地写道:

In externalAllocPossible it simply wrote:

if (currentHeapSize + hs->externalBytesAllocated + n <=
            heap->absoluteMaxSize)
{
    return true;
}
return false;

这意味着一旦如果本机位图分配大小加上currentHeapSize(不会实际分配的大小如下图所示,在这种情况下,它保持堆的最大尺寸,我们遇到,但随后被释放他们全部)超过极限,本机位图的分配总是会失败,但在currentHeapSize在Java中,似乎不会减少,即使91.3%的Java对象的内存已被释放(设为零和触发GC)!

Which means once if the native Bitmap allocation size plus the currentHeapSize(NOT the actually allocated size as shown below, in this case, it's keeping the max size of the heap we bumped up but then freed them all) exceeds the limits, native Bitmap allocation will always fail, but the currentHeapSize in Java seems NOT decrease even when 91.3% Java objects' memory have been freed(set to null and trigger GC)!

有没有其他人遇到这个问题呢?

Is there anybody else met this problem too?

推荐答案

类的位图的拥有的 再循环()方法,描述如下:

The class Bitmap has the recycle() method, described as:

释放与此相关的位图的原生对象...

此方法背后的原因是,有两个堆:Java堆和母语code使用的堆。在GC只看到Java堆大小;对于GC,位图可以看作为一个小的对象,因为它是在Java堆大小小,尽管它引用在本机堆大内存块的事实。

The reason behind this method is that there are two heaps: the Java heap and the heap used by native code. The GC only sees the Java heap sizes; for GC, a bitmap may look as a small object because it's size on the Java heap is small, despite the fact that it references a large memory block in the native heap.

这篇关于位图分配过程中的Andr​​oid 2.x时的Dalvik可能的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆