Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - out of memory exception when creating bitmap

I am getting the following error after creating bitmap second time around:

04-17 18:28:09.310: ERROR/AndroidRuntime(3458): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

this._profileBitmap = Bitmap.createBitmap(_profileBitmap, xCoor,  yCoor, width, height);

From log:

04-17 18:27:57.500: INFO/CameraCropView(3458): Original Photo Size: W 1536 x H 2048   
04-17 18:28:06.170: INFO/CameraCropView(3458): xCoor: 291   
04-17 18:28:06.170: INFO/CameraCropView(3458): yCoor: 430    
04-17 18:28:06.170: INFO/CameraCropView(3458): Width: 952  
04-17 18:28:06.170: INFO/CameraCropView(3458): Height: 952  

Since the image is huge I get the error. But the interesting thing is the error does not happen the first time, only when I take the picture the second time, which makes me believe this profileBitmap is NOT destroyed. How do I clean this up?

like image 954
dropsOfJupiter Avatar asked Apr 18 '11 01:04

dropsOfJupiter


2 Answers

I had the same problem and fix it this way:

My app was ~18MB size, and when I see how much memory left free I was shocked - 654KB (on 1GB RAM!). So I just deleted almost all images from project and downloaded them from the internet on first start, and use pics from SD card when needed.

To check total/free memory for your app use:

 Runtime.getRuntime().totalMemory();
 Runtime.getRuntime().freeMemory();

EDIT: I forgot the main thing - add in your manifest, between application tag, this line:

android:largeHeap="true"

like image 154
Vitas Avatar answered Oct 01 '22 10:10

Vitas


There are many problems with memory exceptions with bitmaps on Android, many of which are discussed on stackoverflow. It would probably be best if you went through the existing questions to see if yours matches one of the existing ones, then if not, write up what makes your situation different.

Some examples:

Out of memory exception due to large bitmap size

Android: out of memory exception in Gallery

Android handling out of memory exception on image processing

etc: https://stackoverflow.com/search?q=android+out+of+memory+exception+bitmap

like image 39
ThomasW Avatar answered Oct 01 '22 11:10

ThomasW