分配变量时Android内存不足

Hou*_*man 5 memory android bitmap

所以我明白了:

'01-01 20:37:34.859: E/dalvikvm-heap(19921): Out of memory on a 6471856-byte allocation.'
Run Code Online (Sandbox Code Playgroud)

尝试将一堆变量分配给内存时.我正在分配24个这样的变量:

mElements.add(new ShopElement(getResources(),R.drawable.shop_starter, b1X , b1Y,true,1,"red",true,5,checkLocked(0)));
Run Code Online (Sandbox Code Playgroud)

其中一个:

bNumbers = new Bitmap[] {Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h0)
        , 65,65, true),
        Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h1)
        , 65,65, true),
        Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h2)
        , 65,65, true),
        Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h3)
        , 65,65, true),
        Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h4)
        ,65,65,true),
        Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h5)
        , 65,65, true),
        Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h6)
        , 65,65, true),
        Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h7)
        , 65,65, true),
        Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h8)
        , 65,65, true),
        Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h9)
        , 65,65, true),
        Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.time)
                , 50,50, true)};
Run Code Online (Sandbox Code Playgroud)

但在我能够通过我创建的24个元素中的第16个之前,它会在某些手机上崩溃.

我有一个Galaxy S2,它运行良好,但在S3和其他手机上它崩溃了.

我很乐意根据要求提供更多信息.

我之前搜索过这个错误,尝试应用解决方案,并查看"有效显示位图"的事情,但似乎没有任何帮助.

旋转的是,它适用于S2,但不适用于S3.S3不应该在各方面超过S2吗?

我使用全局BitmapFactory.Options绘制我的所有位图我设置选项如下:

Global.opts = new BitmapFactory.Options();
    Global.opts.inDither=false;                     
    Global.opts.inPurgeable=true;                  
    Global.opts.inInputShareable=true;              
    Global.opts.inTempStorage=new byte[16 * 1024]; 
Run Code Online (Sandbox Code Playgroud)

Moi*_*med 8

只是你想要获得更多的内存.所以解决方案是增加内存或仅使用可用内存.

三个建议这样做.

1)如果您正在使用BitmapFactory.Options并且仍然存在错误,那么这意味着您没有对给定图像使用它的正确配置.只需确保堆大小足够.

 BitmapFactory.Options opts=new BitmapFactory.Options();
        opts.inDither=false;                     //Disable Dithering mode
        opts.inSampleSize = 8;                    // scale image upto 1/8 of the original image.
        opts.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
        opts.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
        opts.inTempStorage=new byte[16 * 1024]; //you can increase this value as per your need.
Run Code Online (Sandbox Code Playgroud)

您可以使用属性增加inTempStorage大小以使其工作或sample图像inSampleSize.有关采样的更多信息,请参阅此链接.

尝试使用不同的选项.它应该工作.

2)您还可以通过在文件中使用android:largeHeap ="true"属性来增加App的整体堆大小,manifest但这不适用于任何预装的Honeycomb设备.在2.3之前的设备上,您可以使用VMRuntime类.

3)如果您想以编程方式检测堆大小并希望根据需要进行更改,则可以按照此链接进行操作.