Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 target 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?

like image 914
false_azure Avatar asked Mar 26 '15 08:03

false_azure


2 Answers

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

like image 62
ytRino Avatar answered Nov 04 '22 15:11

ytRino


Solved it, the garbage collection message was actually referencing the ImageView, not the Callback object. Ensuring that the ImageView object isn't garbage collected will correct this (e.g. by saving the ImageView in a field in the class, or even the activity that my class was nested in)

like image 2
false_azure Avatar answered Nov 04 '22 13:11

false_azure