false_azure false_azure - 1 year ago 73
Android Question

Issues with garbage collection and Picasso

I am trying to set an ImageView in a Google Maps Marker's InfoWindow and have copied the code from this answer pretty exactly, except that my InfoWindowAdapter isn't an anonymous inner class (it's just an inner class of the activity). This was working before, but for some reason it has stopped working - the onSuccess method in the Callback doesn't get called, so the InfoWindow only displays the image the second time it is opened.

Looking at the logs for Picasso I'm getting messages similar to

Main canceled [R20]+374ms target got garbage collected
. I figured this might be because the Callback is getting gc'd, and tried making it final, and also saving the object in a class field (neither of these worked, although maybe I was doing it wrong?)

What could be happening here, and how can I fix it? Is that
in the error message referring to the Callback, or could it be referring to the marker that gets passed as an argument to the Callback's constructor?

Another odd thing is that sometimes the images are loaded correctly when the InfoWindow is first opened - I'm trying to find out why, but basically I have a lot of markers and whether their images load correctly or not on the first go seems to be inconsistent. There are some (the majority) that never seem to load correctly when the InfoWindow is first opened.

[edit] This was after a bunch of code was merged into that activity, so could it be a memory thing? (there's more processing done now than there was when I wasn't having this problem)

[edit 2] I'm having exactly the same issue with Glide!! Probably garbage collection?

Answer Source

I'm not familiar with that answer, but Target could be gc'ed when you do not hold strong reference to that.

It's because Picasso holds Target instance with weak reference.

You should hold Target instance somewhere outside of Picasso.

Check this issue: https://github.com/square/picasso/issues/352