1

问题出现在最后一行,当我尝试新建这样的数组时,它会崩溃。在 Logcat 中,它显示“java.lang.OutOfMemoryError”。那是什么意思?请帮忙。

Bitmap bmap;
Bitmap bMapRotate;
int bmapWidth;
int bmapHeight;
int[] bmapColorArray;
int bmapColorArraySize;

bmapWidth = bmap.getWidth();
bmapHeight = bmap.getHeight();
bmapColorArraySize = bmapWidth * bmapHeight;
bmapColorArray = new int[bmapColorArraySize];
bmap.getPixels(bmapColorArray, 0, bmapWidth, 0, 0, bmapWidth, bmapHeight);

然后调用将这个 int Array 传递给 native 层。

4

3 回答 3

3

Android 应用程序的可用内存量有限。取决于从 16MB 到 24MB (IIRC) 的屏幕分辨率/大小。

因此,如果您为大图像(10M 像素)分配多个数组,那么您的内存很快就会被填满。

尽量只存储必要的信息。如果您使用的是图像的缩放版本(您可能是)存储缩放版本而不是完整的原始版本。此外,不要将您(可能)不会再次使用的图像保存在内存中。

换句话说,尽量保守你的内存,尤其是大内存分配。

于 2013-01-23T08:00:26.187 回答
0

手动调用垃圾收集器。

System.gc();
于 2013-01-23T09:06:06.353 回答
0

如果您想避免崩溃,请尝试此操作。

 Runtime runtime = Runtime.getRuntime();  

    Log.d("Free memory",""+runtime.freeMemory());  

    if(runtime.freeMemory()>(h*w)) 
    {
        int[] i = new int[h* w];
    }
    else
    {
        Toast.makeText(getApplicationContext(),"No More Heap memory",Toast.LENGTH_LONG).show();
     }
于 2016-05-30T09:56:08.877 回答