Android out of memory when assigning variables

后端 未结 3 1933
北恋
北恋 2020-12-19 11:38

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

相关标签:
3条回答
  • 2020-12-19 12:13

    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.

    0 讨论(0)
  • 2020-12-19 12:20

    The error clearly says that you are trying to use more memory. This directly points to the image sizes. So, you need to resize your images to use lesser memory. One which can be used for this reason is BitmapFactory.Option. Or else go with manually resizing your images take care that it suit all your needs. Just for the sake of explaining things,

    Heap size is the amount of memory allocated to an application to execute. The heap size for android applications are determined by the device RAM. For example if the device has RAM of 179 MB, the android applications will only get the heap size of 18MB.

    Find about it more here. more on how to Load Large Bitmaps Efficiently is found here.

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

    Checkout the RAM size of devices you mentioned.

    0 讨论(0)
  • 2020-12-19 12:20

    In Android Memory Management, every app has a Maximum Heap memory it can use , once the app uses more than the heap memory Android OS will automatically kill's the app. So only you are getting "Out of Memory Exception"..

    To solve this you can go for NDK concept of Android.. Since the heap concept is for the SDK part..

    i.e,

    you can make a App uses more than 300MB (if available) of RAM using NDK but not possible in SDK

    0 讨论(0)
提交回复
热议问题