14

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

Steven Byle
  • 13,149
  • 4
  • 45
  • 57
user4o01
  • 2,688
  • 4
  • 39
  • 54

2 Answers2

52

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.

Tushar
  • 8,019
  • 31
  • 38
Jake Wharton
  • 75,598
  • 23
  • 223
  • 230
  • 1
    Hi Mr. Jake, the application on which i am working currently is largely based on image processing and therefore it needs a significant amount of memory heap. But now using picasso with default caching calculation that covers 15% of the available memory i'm actually running out of memory. So can i use https://github.com/JakeWharton/DiskLruCache for disk caching in picasso? any example or hint? – KunalK Jan 07 '14 at 09:27
  • The HTTP client is already using a disk-based LRU cache so you don't need to. – Jake Wharton Jan 07 '14 at 20:44
  • 1
    Actually my requirement was to access all the full images from SD card and applying custom transformation to generate thumbnail images which need to be cached in SD card rather than internal memory. But no issue now at all because somehow i was able to implement DiskLruCache in LRUCache.java file and it's working too. Thanks :) – KunalK Jan 08 '14 at 14:21
  • 1
    The LRU cache is queried on the main thread. I hope you are not doing disk access in `get()`. – Jake Wharton Jan 08 '14 at 19:58
  • 1
    @JakeWharton you say 1/7 of the heap is reserved for the LruCache but in the memory report the LruCache instance is using more than 40%. I get similar results from MAT; do you have an explanation for this? – Tommaso Barbugli Jan 31 '14 at 10:53
  • 1
    This 40%+ is due to leaks with the application itself or through the picasso's LRUCache being retained somewhere I guess – Lalith B Mar 21 '14 at 07:24
  • Mine is reporting 53% – Tyler Davis Oct 23 '14 at 20:53
  • If I were to use Picasso.Builder, would that set the cache throughout every Picasso instance, or would I have to use a Singleton to use the 'p' object throughout my project? – user1743524 Nov 04 '14 at 14:58
  • Also, I have a project that uses another disk-based image cache, that is not used with Picasso. Could this cause a memory leak; where each cache holds a different reference to a bitmap, so even if the bitmap is cleared from one cache, it remains in the other? – user1743524 Nov 04 '14 at 14:59
  • @TommasoBarbugli Your report shows how much the LRU cache is using out of total used memory, not total available memory. LRU cache is 1/7 of total available memory. So for example, on my N5 with largeheap enabled I have 512MB available for each app so LRU cache should be around 73M. However, if my app is using a total of 140MB it will show up as half of that. – entropy Dec 02 '14 at 12:53
  • hay @JakeWharton i'am using picasso to load images from disk as follow picasso.with(context).load(file).into(imageview); ...... how can i set memory cash this way? thanx in advance – Error Nov 14 '15 at 18:25
  • @user1743524 `Picasso.setSingletonInstance(p);` in Application class – hmac Jan 31 '17 at 16:36
  • for configuration of disk caching I think you need to set your own downloader, see: http://stackoverflow.com/questions/23978828/how-do-i-use-disk-caching-in-picasso – hmac Jan 31 '17 at 16:38
  • its 1/7 by default, so if i say LruCache(24000) how much has it changed? what is the max i can allot? – Siddarth G Jan 22 '18 at 23:07
4

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);
Praveen
  • 388
  • 3
  • 7