分配变量时Android内存不足 [英] Android out of memory when assigning variables

查看:24
本文介绍了分配变量时Android内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我明白了:

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

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

When trying to assign a bunch of variables to memory. I'm assigning 24 variables like these:

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

其中之一:

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)};

但在我完成我创建的 24 个元素中的第 16 个之前,它在某些手机上崩溃了.

But before I can get through #16 of the 24 elements I'm created, it crashes on certain phones.

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

I have a Galaxy S2 and it runs fine, but on the S3 and other phones it crashes.

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

I will be happy to provide more information upon request.

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

I've searched this error before, tried to apply solutions, and looked through the "Displaying Bitmaps Efficiently" thing, but nothing seems to help.

让我头疼的是它适用于 S2,但不适用于 S3.S3不应该在各方面都超过S2吗?

What spins my head is that it works on the S2, but not the S3. Shouldn't the S3 exceed the S2 in every way?

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

I am using a global BitmapFactory.Options to draw all my bitmaps I set the options as follows:

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

推荐答案

只是您试图访问更多内存.所以解决方案是增加内存或仅使用可用内存.

Simply you are trying to access more memory then you have. So solution would be to either increase memory or use only available memory.

这样做的三个建议.

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

1) If you are using BitmapFactory.Options and still error persist then it means that you are not using proper configuration of it for the given image. Just Make sure that Heap size is sufficient.

 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.

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

You can increase the inTempStorage size to make it work or sample the image by using inSampleSize attribute. For more on sampling, you can refer this link.

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

Try playing with different options. It should work.

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

2) You can also increase you App's overall Heap size by using android:largeHeap="true" atribute in manifest file but this will not work on any pre Honeycomb devices. On pre 2.3 devices, you can use the VMRuntime class.

3) 如果你想以编程方式检测你的堆大小并想根据需要改变它,那么你可以按照这个 链接.

3) If you want to programmatically detect you Heap size and want to change it according to the need then you can follow this link.

这篇关于分配变量时Android内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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