Do we have to explicitly recycle the bitmap if we don't need it?
Asked Answered
M

1

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?

Muchness answered 21/9, 2012 at 14:55 Comment(0)
A
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.

Ashelyashen answered 21/9, 2012 at 15:2 Comment(3)
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.0Amiamiable
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++).Ashelyashen
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 quicklyAmiamiable

© 2022 - 2024 — McMap. All rights reserved.