Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawable advantage over bitmap for memory in android

This question is linked with the answers in the following question:

Error removing Bitmaps[Android]

Is there any advantage of using Drawable over Bitmap in Android in terms of memory de-allocation ?

I was looking at Romain Guy project Shelves and he uses SoftReference for images caches but I'm unable to search where is the code which is de-allocating these Drawables when SoftReference automatically reclaims the memory for Bitmap. As far as I know .recycle() has to be explicitly called on the Bitmap for it to be de-allocated.

like image 430
2cupsOfTech Avatar asked Dec 31 '10 15:12

2cupsOfTech


2 Answers

In my understanding, Bitmaps are typically better for performance if you don't need to do much image manipulation. However, I have run into memory leaks when I don't manually recycle them. My solution was to write a class to help me manage my images that provides an easy way to recycle all my bitmaps at certain points in my application. It also provides an easy way to reuse already loaded resources (including Drawables).

like image 145
devnate Avatar answered Dec 14 '22 04:12

devnate


You don't need to call Bitmap.reycle(). This will be done for you in its finalizer. Doing it in the finalizer means the allocation will be delayed until finalizers run, so when possible directly calling recycle() can help with memory management.

like image 39
hackbod Avatar answered Dec 14 '22 03:12

hackbod