Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to clear heap?

Tags:

android

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

like image 998
Sando Avatar asked May 02 '11 05:05

Sando


2 Answers

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
like image 179
Sando Avatar answered Oct 15 '22 13:10

Sando


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:

  • http://markmail.org/message/smg7pog5tz25p7w5
  • External allocation too large for this process in Android
  • Strange out of memory issue while loading an image to a Bitmap object
  • http://code.google.com/p/android/issues/detail?id=2822
  • Bitmap, Bitmap.recycle(), WeakReferences, and Garbage Collection
  • How to deal with "java.lang.OutOfMemoryError: Java heap space" error (64MB heap size)

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.

like image 23
Aleadam Avatar answered Oct 15 '22 13:10

Aleadam