Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley NetworkImageView - how to recycle bitmaps?

What I miss in this fantastic library is the fact it does not recycle bitmaps in NetworkImageView. It would be perfect if in method NetworkImageView.onDetachedFromWindow the recycle was called somewhere. Currently the library has this serious leak of not recycling bitmaps and running into OOM exceptions. Does anybody know whether there is a plan for integrating this concept?

like image 819
cubesoft Avatar asked Jun 28 '13 08:06

cubesoft


1 Answers

By "recycle bitmaps", I assume you mean calling .recycle() on the Bitmap object.

Why do you believe this would improve your OOM situation? While I'll admit that once upon a time, Android (i.e. 2.x) really suffered from this problem. Bitmaps were stored in the native heap and required (at least) 2 GC passes to cleanup. Calling recycle() sometimes improved the situation.

Since Android 4.x3.0, this has been less of a problem.

Note that the documentation considers this an advanced call that shouldn't normally be used: http://developer.android.com/reference/android/graphics/Bitmap.html#recycle()

As for your recommendation to recycle the Bitmap in onDetachedFromWindow(), this seems like a bad idea. This method is called when the View is detached from the window, there's no guarantee that the View isn't re-attached later. If we were to recycle the Bitmap in onDetachedFromWindow(), we'd throw an exception if the View is ever re-attached to the Window.

Besides, if this was required, I'd think that ImageView itself would handle it.

And for the record, Volley handles OOM fairly well by trying to scale the image within the View's bounds and only allowing one thread at a time to decode the bitmap.

I suspect you're having other issues that are causing your OOM...

------------ EDIT ---------------

After android developer's comment comment, I've updated my answer. Volley doesn't automatically scale the image within the View's bounds, but it does have support for it in ImageRequest (see constructor: https://android.googlesource.com/platform/frameworks/volley/+/master/src/com/android/volley/toolbox/ImageRequest.java with maxWidth and maxHeight)

This means you'd have to modify NetworkImageView to use the right ImageRequest constructor (you'd have to be careful to ensure the view is measured first)

like image 151
kwazi Avatar answered Oct 23 '22 01:10

kwazi