分配变量时出的Andr​​oid内存 [英] Android out of memory when assigning variables

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

问题描述

所以,我得到这样的:

  '01 -01 20:37:34.859:E / dalvikvm堆(19921):内存不足。在6471856字节分配
 

在尝试了一些变量分配到内存中。 我分配24个变量这样的:

  mElements.add(新ShopElement(getResources(),R.drawable.shop_starter,B1X,b1Y,真正的,1,红,如此,5,checkLocked(0)) );
 

和其中的一个:

  bNumbers =新位图[] {Bitmap.createScaledBitmap(BitmapFactory.de codeResource(getResources(),R.drawable.h0)
        ,65,65,真),
        Bitmap.createScaledBitmap(BitmapFactory.de codeResource(getResources(),R.drawable.h1)
        ,65,65,真),
        Bitmap.createScaledBitmap(BitmapFactory.de codeResource(getResources(),R.drawable.h2)
        ,65,65,真),
        Bitmap.createScaledBitmap(BitmapFactory.de codeResource(getResources(),R.drawable.h3)
        ,65,65,真),
        Bitmap.createScaledBitmap(BitmapFactory.de codeResource(getResources(),R.drawable.h4)
        ,65,65,真),
        Bitmap.createScaledBitmap(BitmapFactory.de codeResource(getResources(),R.drawable.h5)
        ,65,65,真),
        Bitmap.createScaledBitmap(BitmapFactory.de codeResource(getResources(),R.drawable.h6)
        ,65,65,真),
        Bitmap.createScaledBitmap(BitmapFactory.de codeResource(getResources(),R.drawable.h7)
        ,65,65,真),
        Bitmap.createScaledBitmap(BitmapFactory.de codeResource(getResources(),R.drawable.h8)
        ,65,65,真),
        Bitmap.createScaledBitmap(BitmapFactory.de codeResource(getResources(),R.drawable.h9)
        ,65,65,真),
        Bitmap.createScaledBitmap(BitmapFactory.de codeResource(getResources(),R.drawable.time)
                ,50,50,真)};
 

但在此之前,我可以通过#16我创建的24个元素的获得,它崩溃的某些手机。

我有一个银河S2 并运行良好,但在 S3 等手机崩溃。

我会很乐意的要求,提供更多的信息。

我以前搜查这个错误,尝试应用解决方案,并期待通过显示位图高效的事情,但似乎没有任何帮助。

什么旋转我的头,它的作品在S2,而不是S3。如果不是S3超过S2以各种方式?

我使用的是全球BitmapFactory.Options画出我所有的位图我设置的选项如下:

  Global.opts =新BitmapFactory.Options();
    Global.opts.inDither = FALSE;
    Global.opts.inPurgeable = TRUE;
    Global.opts.inInputShareable = TRUE;
    Global.opts.inTempStorage =新的字节[16 * 1024];
 

解决方案

只要你想,那么你必须访问更多的内存。因此,的解决办法是要么增加内存或只使用可用内存

三建议这样做。

1)如果你使用 BitmapFactory.Options ,仍然坚持错误那么就意​​味着你不使用它适当的配置对于给定的图像。 只是确保堆大小足以

  BitmapFactory.Options选择采用=新BitmapFactory.Options();
        opts.inDither = FALSE; //禁用抖动模式
        opts.inSampleSize = 8; //高达原始图像的1/8比例的图像。
        opts.inPurgeable = TRUE; //告诉以GC,无论它需要释放内存,位图可被清除
        opts.inInputShareable = TRUE; //哪一种参考将用于被明确后恢复的位图数据,什么时候会在将来被使用
        opts.inTempStorage =新的字节[16 * 1024]; //你可以增加这个值根据您的需要。
 

您可以增加 inTempStorage 尺寸,使其工作或样品图像通过使用 inSampleSize 属性。欲了解更多关于抽样的,你可以参考这个<一href="http://stackoverflow.com/questions/3331527/android-resize-a-large-bitmap-file-to-scaled-output-file/8497703#8497703">link.

请尝试使用不同的选择播放。它应该工作。

2)您也可以使用<一个增加你的应用程序的整体堆大小href="http://developer.android.com/reference/android/R.styleable.html#AndroidManifestApplication_largeHeap">android:largeHeap="true"属性附加伤害的清单文件,但是这不会对任何pre蜂窝设备的正常工作。在pre 2.3装置,您可以使用VMRuntime类。

3)如果您希望以编程方式检测到您堆大小,并希望根据需要改变它,那么你可以关注该<一href="http://stackoverflow.com/questions/2630158/detect-application-heap-size-in-android">link.

So I get this:

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

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

And one of these:

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

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

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.

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?

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.

Three suggestions to do so.

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.

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) 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) If you want to programmatically detect you Heap size and want to change it according to the need then you can follow this link.

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

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