2cupsOfTech
2cupsOfTech

Reputation: 6073

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.

Upvotes: 11

Views: 7827

Answers (3)

0xC0DED00D
0xC0DED00D

Reputation: 20348

Acc. to this page, starting from API Level 11, the Bitmap pixel data is stored in the Dalvik Heap along with the associated Bitmap. Thus calling .recycle is actually not required, unless you want to reclaim the memory manually for further use. Be sure to de-reference the bitmap too, just as an added measure.

PS: This was the link which explains hackbod's answer.

Upvotes: 0

devnate
devnate

Reputation: 764

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

Upvotes: 10

hackbod
hackbod

Reputation: 91331

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.

Upvotes: 9

Related Questions