简体   繁体   中英

bitmap size exceeds VM budget java.lang.OutOfMemoryError: bitmap size exceeds VM budget

I know this question is already asked in this forum many times but still I cann't get the perfect answer which is solve this issue.

In my application I am using same image in 3 different sizes: small, medium, large.

When I use many times this images I got this error.

08-24 11:08:55.994: E/ImageLoader(636): bitmap size exceeds VM budget
08-24 11:08:55.994: E/ImageLoader(636): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
08-24 11:08:55.994: E/ImageLoader(636):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-24 11:08:55.994: E/ImageLoader(636):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
08-24 11:08:55.994: E/ImageLoader(636):     at com.nostra13.universalimageloader.core.ImageDecoder.decode(ImageDecoder.java:75)
08-24 11:08:55.994: E/ImageLoader(636):     at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeWithOOMHandling(LoadAndDisplayImageTask.java:161)
08-24 11:08:55.994: E/ImageLoader(636):     at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:148)
08-24 11:08:55.994: E/ImageLoader(636):     at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:104)
08-24 11:08:55.994: E/ImageLoader(636):     at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:58)
08-24 11:08:55.994: E/ImageLoader(636):     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
08-24 11:08:55.994: E/ImageLoader(636):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-24 11:08:55.994: E/ImageLoader(636):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-24 11:08:55.994: E/ImageLoader(636):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
08-24 11:08:55.994: E/ImageLoader(636):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
08-24 11:08:55.994: E/ImageLoader(636):     at java.lang.Thread.run(Thread.java:1096)

Can anyone help to sort it out.. I already searched a lot for the same issue, but still I am not getting such a solution..

Thats why I have to write the question in this four-am.

I am waiting for positive reply..

Thanks.

This is a common problem with Images. Android gives each App a set amount of space. Images when taken as they are without downsizing tend to cause this problem. The best solution is to

BitmapFactory.Options options = new BitmapFactory.Options();

options.inSampleSize = 8; (Use values here as powers of 2 for best solutions)

and pass them to Bitmaps when you will decode

Bitmap bm = BitmapFactory.decodeStream(<input image file>, null, options);

I think you can use a fail retry mechanism to solve this issue.

Like : Try to load the image as normal, if fails, from the catch block, try to load it with reduced quality using BitmapConfig (Bitmap.Config.ARGB_4444), if again fails, try to load it with even lesser quality (Bitmap.Config.ALPHA_8). I also had the same issue and now this method works for me.

Bitmap imageBitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = false; 
options.inPurgeable = true;
options.inInputShareable = true; 
options.inTempStorage = new byte[32 * 1024];
options.inPreferredConfig = config;//Bitmap.Config.ARGB_4444 or Bitmap.Config.ALPHA_8 when failed  
options.inSampleSize = 1;
imageBitmap = BitmapFactory.decodeFile(bitmapPath, options);

You can use this method to decode a bitmap:

public static Bitmap loadBitmap() {
    FileInputStream fis = null;
    Bitmap bitmap = null;

    BitmapFactory.Options option = new BitmapFactory.Options();
    option.inDither = false;
    option.inPurgeable = true;
    option.inInputShareable = true;
    option.inTempStorage = new byte[32 * 1024];
    option.inPreferredConfig = Bitmap.Config.RGB_565;

    try {
        fis = IOHelper.openInputStream(); //get the picture's FileInputStream
        FileDescriptor fd = fis.getFD();
        bitmap = BitmapFactory.decodeFileDescriptor(fd, null, option);
    } catch (IOException e) {

    } finally {
        IOHelper.closeQuietly(fis);
    }
    return bitmap;
}
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget

This means that the device has gone out of memory and it is being force closed.

Now, since you say that the error is coming when you use the images many times, I believe that you are not freeing the resources. Bitmaps can sometime take up a lot of space and if you are persisting them in the memory, I would not be surprised if the device goes OOM. Different devices have different specs for memory and after a few runs it is filling up the memory.

So, my suggestion would be to clear the Bitmaps and the other storage that you are using for temporary purposes.

If that still does not solve your problem, post the code where you are handling the images and we will try to see if there is a more memory efficient way of what you are doing.

Good luck!

This is an old question but I've ended up wasting a whole day trying to fix image memory problems on older devices using the excellent UIL. None of these answers are specific to UIL, my solution finally came from stumbling across this question answered by Nostra himself:

How can I resize a picture before displaying it in a view with Android Universal Image Loader?

The maxImageWidthForMemoryCache and height values here for me were very important. I was downloading and displaying an image approximately the size of the screen and placing it in an image view the width and height of the screen so no scaling whatsoever was actually taking place. This was too large for my 2.3 phone and cutting the dimensions in half helped a great deal with little noticeable image degradation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM