How to avoid createBitmap () crash in android

I use createBitmap() in several places. Several times this api throws an OutOfMemoryError() exception. How to avoid this exception?

I use as shown below

 createBitamp(width, height, Config.ARGB_8888); 

width = width screen

height = height screen

Any help would be appreciated.

+7
source share
2 answers

I posted some info on how bitmaps are handled in the next Android release ticket. This may be useful for you: http://code.google.com/p/android/issues/detail?id=8488#c80

+6
source

Many are faced with this problem. You have three ways to solve this problem:

  • Increase available memory: stop services or change your device to a newer one.
  • Reduce memory usage: by optimizing code
  • [UPDATE] Free up unused bitmap memory: call recycle () .
  • [UPDATE] Do not use GarbageCollector :)

Usually with Bitmap problems, a garbage collector will help.

Justin Breitfeller's answer links to a more detailed explanation of the internal workings of Bitmap. The message that needs to be removed is that the memory allocated for the bitmap image data (in the createBitmap native method) is processed separately and is not freed directly by GarbageCollector when Bitmap becomes a garbage collector. The real solution is to recycle () your bitmaps when you are not using them. This will still retain the (small) memory allocated for the Bitmap object, but mark the (large) memory allocated for the garbage collector. Consequently, the GarbageCollector in turn frees both, but you do not need to call it manually before OutOfMemory happens, the JVM will try the GarbageCollect anyway.

+5
source

All Articles