0

I have a FragmentStatePagerAdapter with a bunch of Fragments that are loaded and destroyed as the user swipes around. The Fragments 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();
}
Community
  • 1
  • 1
weirdo16
  • 337
  • 5
  • 13
  • 1
    This should be a similar connection to your problem: http://stackoverflow.com/questions/27502659/clear-cache-memory-of-picasso – Fomovet Sep 21 '16 at 00:28
  • @Xzuing -- thanks, I hadn't thought of recycle()'ing the Bitmaps. I'm still not making any progress though. – weirdo16 Sep 22 '16 at 01:02

0 Answers0