OutOfMemoryError: bitmap size exceeds VM budget :- Android

60,745

Solution 1

Use decodeStream(is, outPadding, opts) with

BitmapFactory.Options opts=new BitmapFactory.Options();
opts.inDither=false;                     //Disable Dithering mode
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[32 * 1024]; 

Solution 2

You could check the image size and then downsample it by appropriate factor.

See this question: Handling large Bitmaps

Solution 3

This issue seems to have been reported several times, here and here for instance... sorry Shalini but if it's the same issue, it seems that there is no solution at all...

The only advice of Romain Guy is to use less memory...

So, good luck to think your stuff differently...

Solution 4

Finally, after resample the image as suggested above, you may call bitmap_file.recycle().

Share:
60,745
Andy
Author by

Andy

Updated on March 13, 2020

Comments

  • Andy
    Andy over 4 years

    Possible Duplicate:
    Android: Strange out of memory issue while loading an image to a Bitmap object

    i am downloading images from Url and displaying them. At download time it is giving out of memory error : bitmap size exceeds VM budget. I am using drawable. Code is below:

    HttpClient httpclient= new DefaultHttpClient();
    HttpResponse response=(HttpResponse)httpclient.execute(httpRequest);
    HttpEntity entity= response.getEntity();
    BufferedHttpEntity bufHttpEntity=new BufferedHttpEntity(entity);
    InputStream instream = bufHttpEntity.getContent();
    
    Bitmap bm = BitmapFactory.decodeStream(instream);
    Bitmap useThisBitmap = 
    Bitmap.createScaledBitmap(bm,bm.getWidth(),bm.getHeight(), true);
    bm.recycle();
    BitmapDrawable bt= new BitmapDrawable(useThisBitmap);
    System.gc();
    

    Here is the error:

    `05-28 14:55:47.251: ERROR/AndroidRuntime(4188): 
     java.lang.OutOfMemoryError: bitmap size exceeds VM budget`
    
  • Romain Guy
    Romain Guy about 14 years
    There is a solution: use less memory than the maximum allowed. I know this is a frustrating answer but phones do not have "unlimited" amount of RAM like desktop machines. You have to be careful.
  • Sephy
    Sephy about 14 years
    Didn't know you were coming on stack ;) As I said, your recommandation was to use less memory... What would you advise to do for that? reduce the size of the picture?
  • caw
    caw over 12 years
    This doesn't explicitly call the garbage collector but it recommends the virtual machine to do it.
  • Christopher Perry
    Christopher Perry over 11 years
    If you're leaking memory this will do absolutely nothing. Garbage collections only recycle objects that don't have anything else hanging onto a reference to them.
  • Christopher Perry
    Christopher Perry over 11 years
    Requesting garbage collection will not clean up a memory leak.
  • jucajl
    jucajl almost 11 years
    Thanks, my application is working now!
  • sanjeev
    sanjeev almost 11 years
    Thanks, was searching for a solution for months
  • user253751
    user253751 almost 4 years
    @RomainGuy P.S. it's now 2020 and my phone has 4GB of RAM