Android ImageView setImageBitmap
Asked Answered
S

1

2

I'm trying to better understand how Android handle images in order to use memory more efficiently. I have an image stored in the Bitmap and I'm using ImageView.setImageBitmap() to display it. Now the question is - will it use the Bitmap I've passed it in the future, or it's making a copy of it and the Bitmap I've created isn't used anymore after the call to setImageBitmap?

Asuming it's gonna keep reference to the Bitmap I've passed, how is it gonna behave when Bitmap was created via BitmapFactory using inPurgeable option? Will ImageView prevent the Bitmap from being temporarily purged from the memory? Is it gonna happen only when ImageView has View.VISIBLE state, or also when View.GONE and View.INVISIBLE? Or maybe only while ImageView is visible on the screen?

And one more thing - looking through Android source code reveals that encoded byte data is always copied into memory (inInputShareable is currently ignored). Is it counted towards the 16/24MB memory limit for android Java application?

Thanks

Stroboscope answered 29/10, 2011 at 12:4 Comment(4)
I think you can answer some of your questions by simple experiments. I would say the bitmap you pass to setImageBitmap() is used directly. And, no matter if a view is gone or not, if it refers to it, it will never be destroyed by garbage collector. Pozdro ;)Amalee
Unfortunetely it's not that simple. Even if ImageView keep the reference to passed Bitmap, it doesn't mean that data stored in the Bitmap cannot be purged. According to the docs "resulting bitmap will allocate its pixels such that they can be purged if the system needs to reclaim memory", "when the pixels need to be accessed again(..), they will be automatically re-decoded". What I've asked about is whether ImageView will prevent the decoded data from being purged, or it will be purged and re-decoded when needed. There's no easy way to find out and it's behaviour can depend on several factors.Stroboscope
"when the pixels need to be accessed again(..), they will be automatically re-decoded" - what if an ImageView has been set a bitmap as it was, created runtime, without any information where it was taken from. If that was true, it would be gone forever. I don't think so.Amalee
You're wrong again. When Bitmap is created via BitmapFactory using inPurgeable (and that's the case we're discussing), a copy of encoded data is also stored. I'm under impression you've forgot Bitmap is a complex class with lots of data, not a simple variable with decoded picture. Refer to the docs for further info: developer.android.com/reference/android/graphics/…Stroboscope
S
0

Take a look at this article : http://developer.android.com/training/displaying-bitmaps/index.html

There's some useful lessons that helps you to understand android's memory management better

Stingaree answered 21/1, 2014 at 9:38 Comment(2)
Note that link-only answers are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference.Geniality
Well initially I was trying to leave a comment instead of an answer like this. However this site is seriously NOT friendly to new users at all, 50 reputation just for a comment? jeezStingaree

© 2022 - 2024 — McMap. All rights reserved.