6

I am working on a camera application. For first time if I capture image its working fine but if I take a picture again its throwing a error

ERROR/dalvikvm-heap(2398): 10077696-byte external allocation too large for this process." VM won't let us allocate 10077696 bytes" and finally"05-02 05:35:38.390: ERROR/AndroidRuntime(2398): FATAL EXCEPTION: main 05-02 05:35:38.390: ERROR/AndroidRuntime(2398): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

and application force closes..how to handle this how to clear heap and vm? please help.. Thanks in advance..

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
Sando
  • 1,893
  • 11
  • 29
  • 45

2 Answers2

4

I found the answer.
I used the following code:

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

                bfOptions.inDither=false;                     //Disable Dithering mode

                bfOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared

                bfOptions.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

                bfOptions.inTempStorage=new byte[32 * 1024]; 
   CameraTricks.SdCardImage= BitmapFactory.decodeFile(CameraTricks.yu,bfOptions);
CameraTricks.yu is my path to bitmap
Adinia
  • 3,722
  • 5
  • 40
  • 58
Sando
  • 1,893
  • 11
  • 29
  • 45
  • I tried this option, this seems to work for API 2.3. But not in APIs less than 2.3. The problem seems to be with this line, `bfOptions.inTempStorage=new byte[32 * 1024]; ` VM doesn't allow us to increase its storage capacity. – Andro Selva Jun 01 '11 at 09:25
  • NO IT ALSO WORKS FOR 2.2 AND 2.1 WITHOUT ANY PROBLEM – Sando Jun 01 '11 at 10:27
1

You don't. You can soft reset the device, but I doubt that will do any good. Android's garbage collector should take care of it.

Most probably, your app is using too much memory for some operation. You can use DDMS to check memory consumption (read about it here).

You can read about similar issues in all these links:

It looks like a common theme is the loading of several large images. Make sure you don't keep references to images (or any other large object) you don't use any more, so the garbage collector can recover that memory. Use Bitamp.recycle(), for example.

Lastly, make sure you read the article Avoiding Memory Leaks.

Community
  • 1
  • 1
Aleadam
  • 40,203
  • 9
  • 86
  • 108