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

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 ;) Commented Oct 29, 2011 at 12:09
  • 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. Commented Oct 29, 2011 at 12:31
  • "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. Commented Oct 29, 2011 at 12:38
  • 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/… Commented Oct 29, 2011 at 13:39

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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? jeez

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.