2

Bitmap has a recycle method, but do we have to invoke it explicitly if we don't need it any more?

For example, an ImageView has a bitmap now. When user click a button, it will set a new bitmap to the ImageView.

Do we have to recycle the original bitmap before assign the new one?

Freewind
  • 193,756
  • 157
  • 432
  • 708

1 Answers1

3

yes you have if you are targeting devices with Android older the 3.0. That's will avoid you to incour in the OutOfMemoryException.

Note: Before android 3 the Bitmap memory is allocated in the native heap. The java object will retains low memory from the GC perspective.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • I don't believe this is entirely true, it depends on how you bound a bitmap to the imageview in the first place, and what size that bitmap might be to begin with, I would not go calling recycle() just because its pre android 3.0 – Ian Warwick Sep 21 '12 at 15:16
  • well of course that depends on its size. But do not recycle() it means leak of native memory. It is how when you forget to call delete after a new (c++). – Blackbelt Sep 21 '12 at 15:25
  • 2
    Well the GC will eventually clean up so its not exactly a memory leak since the memory is recovered by GC, its just an issue with the GC not freeing critical bitmap memory quick enough, however in usual circumstances such as binding icons in lists its proved to be ok for me so far, the only time I ran into bitmap budget issues was with swapping large images quickly – Ian Warwick Sep 21 '12 at 15:37