Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Picasso Configure LruCache Size

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?

enter image description here

like image 481
user4o01 Avatar asked Nov 20 '13 07:11

user4o01


2 Answers

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.

like image 105
Jake Wharton Avatar answered Oct 22 '22 10:10

Jake Wharton


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);
like image 36
Praveen Avatar answered Oct 22 '22 10:10

Praveen