Android Picasso Configure LruCache Size

2019-01-21 21:12发布

问题:

I am using the trending Picasso in my Project, but I have dumped the heap and it looks like this. Now yesterday it gives me 48M for LruCache used in Picasso.

How could I specify the size of it?

Note: my loaded images are apparently large.

If someone came up with fit() or centerCrop(), I've read that those functions reduce image size, right? But sometimes I have to display small images in the ListView in full view.

Now, do those functions cache a scaled down image?

回答1:

By default, Picasso uses 1/7th of the available heap for it's LRU. This is a "happy" fraction that works best on all devices well enough.

You can configure the size of the memory cache by passing a custom instance to Picasso.Builder. It can be an instance of the LruCache which takes a max size or any other instance of Cache.

Picasso p = new Picasso.Builder(context)
    .memoryCache(new LruCache(24000))
    .build();

Before you go shrinking this cache size, however, remember that keeping Bitmap instances in RAM allows them to be instantly displayed. Unused RAM is wasted RAM. The memory cache should use as much RAM as possible without causing OOMs (obviously) or unnecessary GC to free space.



回答2:

Below Code Will Increase Picasso Cache Size To 250 MB.

Picasso picasso =  new Picasso.Builder(this).downloader(new OkHttpDownloader(getCacheDir(), 250000000)).build();
Picasso.setSingletonInstance(picasso);