Android Picasso 配置 LruCache 大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20090265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Picasso Configure LruCache Size
提问by user4o01
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.
我在我的项目中使用了趋势毕加索,但我已经倾倒了堆,它看起来像这样。现在昨天它给了我 48MLruCache
用于毕加索。
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.
如果有人想出了fit()
或centerCrop()
,我已经读过这些函数会减小图像大小,对吗?但有时我必须ListView
在全视图中显示小图像。
Now, do those functions cache a scaled down image?
现在,这些函数是否缓存缩小的图像?
回答by Jake Wharton
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.
默认情况下,Picasso 使用 1/7 的可用堆作为它的 LRU。这是一个“快乐”的分数,在所有设备上效果最好。
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.Builder
. 它可以是具有LruCache
最大大小的 的实例,也可以是 的任何其他实例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.
但是,在缩小此缓存大小之前,请记住将Bitmap
实例保存在 RAM 中可以立即显示它们。未使用的 RAM 是浪费的 RAM。内存缓存应该使用尽可能多的 RAM,而不会导致 OOM(显然)或不必要的 GC 来释放空间。
回答by Praveen
Below Code Will Increase Picasso Cache Size To 250 MB.
下面的代码会将毕加索缓存大小增加到 250 MB。
Picasso picasso = new Picasso.Builder(this).downloader(new OkHttpDownloader(getCacheDir(), 250000000)).build();
Picasso.setSingletonInstance(picasso);