Android Picasso Configure LruCache Size
Asked Answered
S

2

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

Sutherland answered 20/11, 2013 at 7:53 Comment(0)
O
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.

Octaviooctavius answered 20/11, 2013 at 20:4 Comment(14)
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 github.com/JakeWharton/DiskLruCache for disk caching in picasso? any example or hint?Daw
The HTTP client is already using a disk-based LRU cache so you don't need to.Octaviooctavius
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 :)Daw
The LRU cache is queried on the main thread. I hope you are not doing disk access in get().Octaviooctavius
@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?Wysocki
This 40%+ is due to leaks with the application itself or through the picasso's LRUCache being retained somewhere I guessFamiliarity
Mine is reporting 53%Kissable
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?Shantell
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?Shantell
@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.Janey
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 advanceLooker
@Shantell Picasso.setSingletonInstance(p); in Application classCorncob
for configuration of disk caching I think you need to set your own downloader, see: #23979328Corncob
its 1/7 by default, so if i say LruCache(24000) how much has it changed? what is the max i can allot?Abigael
I
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);
Impart answered 8/4, 2016 at 5:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.