1

Frequently we get image from server as byte stream, then we create Bitmap and assign it to ImageView object.

When I need to release memory, I can drop all ImageView references to null, letting the gc do the work.

But question is, in this way, I did not call the recycle method of Bitmap since I did not keep references of it. So I wonder that, do I need to maintain all the references of Bitmap, to recycle them in the future?

Lyn
  • 699
  • 1
  • 7
  • 17

2 Answers2

4

No you don't need to, the GC will do it for you.

Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • Sorry but if as you mentioned, why there be a recycle method? I can simply drop the reference to null and let GC recycle it. – Lyn Mar 16 '11 at 05:59
  • 1
    recycle() is here if you manipulate large bitmaps often and need to free their memory as soon as possible. In a normal application this is most of the time unnecessary. – Romain Guy Mar 16 '11 at 17:08
  • @RomainGuy but Bitmaps are allocated in native heap, how GC will get them? – Dmitry Zaytsev Jun 14 '12 at 22:07
  • 1
    When the Bitmap object is reclaimed by the GC it runs native code to clean up its native memory. Note that as of Android 3.0, bitmaps allocate their data on Dalvik's heap. – Romain Guy Jun 15 '12 at 01:05
0

It's better to do so, because Bitmap object itself is poor candidate for GC (since memory allocated in native heap).

Also you should recycle your ImageView bitmap, before using the new one

Recycle ImageView's Bitmap

Community
  • 1
  • 1
Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146