Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitmap recycle() in java Android

Need some help for understanding the recycle() method of the class Bitmap.

If I have a Bitmap[] named "bmp" for example whats the difference between doing

Bitmap[i].recycle()

And

Bitmap[i]=null;

Whats the best option? Should I call both?

Thanks

like image 344
Addev Avatar asked Aug 02 '11 21:08

Addev


People also ask

What is bitmap recycle?

If you're displaying large amounts of bitmap data in your app, you're likely to run into OutOfMemoryError errors. The recycle() method allows an app to reclaim memory as soon as possible. Caution: You should use recycle() only when you are sure that the bitmap is no longer being used.

What are bitmaps in Android?

A bitmap is simply a rectangle of pixels. Each pixel can be set to a given color but exactly what color depends on the type of the pixel. The first two parameters give the width and the height in pixels. The third parameter specifies the type of pixel you want to use.

How do you clear a bitmap?

You can use eraseColor on bitmap to set its color to Transparent. It will useable again without recreating it.

How do I know if my Android BMP is empty?

You can do a check when you want to return the BitMap look to see if the ArrayList of Paths is bigger than 0 and return the BitMap if so, or else return null.


1 Answers

According to this question, bitmap data is stored in native memory rather than in the Dalvik heap. You should call recycle() to free the memory the bitmap is stored in once you are finished with it. It's good practice to set it to null afterwards, although this is somewhat redundant.

See also the api: http://developer.android.com/reference/android/graphics/Bitmap.html#recycle()

like image 182
shanet Avatar answered Sep 27 '22 19:09

shanet