I have a FragmentStatePagerAdapter
with a bunch of Fragment
s that are loaded and destroyed as the user swipes around. The Fragment
s each contain some text, and some very large images. I'm using Picasso to load and cache the images, and each Fragment has its own single instance of Picasso, which is shutdown in onDestroy()
.
When onDestroy()
is called for each Fragment, I'd also like to totally clear the memory cache associated with the Picasso instance. I've tried creating a PicassoTools
class like this answer says, and while that empties the cache (according to the debugger,) it doesn't seem to release the memory associated with the cache, according to the memory monitor. Here's my code for onDestroy()
:
@Override
public void onDestroy() {
PicassoTools.clearCache(picasso);
//release cache memory here somehow
picasso.shutdown();
super.onDestroy();
}
After I clear the cache, how can I totally release all the memory associated with it?
UPDATE: Here's my PicassoTools.clearCache() method, which is called onDestroy()
. I added Bitmap recycle()
ing, but that didn't seem to make a difference.
public static void clearCache(Picasso p) {
ArrayList<String> keys = new ArrayList<>();
keys.addAll(((LoopableLruCache) p.cache).keySet()); //LoopableLruCache is an extension of Picasso's LruCache, just with a keySet() method for looping through it easier
for (String key : keys) {
Bitmap bmp = p.cache.get(key);
if (!bmp.isRecycled()) {
bmp.recycle();
}
bmp = null;
p.invalidate(key);
}
p.cache.clear();
}