计算LruCache的最佳大小

时间:2012-06-21 16:49:39

标签: android

我可以用一个公式来计算LruCache的大小吗? 我可以基于Runtime.getRuntime()提供的VM内存统计数据。

我正在存储位图文件 - 文件数据,而不是解压缩的位图。

2 个答案:

答案 0 :(得分:4)

此处摘录自Caching Bitmaps指南:

// Get memory class of this device, exceeding this amount will throw an
// OutOfMemory exception.
final int memClass = ((ActivityManager) context.getSystemService(
        Context.ACTIVITY_SERVICE)).getMemoryClass();

// Use 1/8th of the available memory for this memory cache.
final int cacheSize = 1024 * 1024 * memClass / 8;

答案 1 :(得分:1)

您正在考虑解决问题的方法。您不应该尝试使缓存适合可用内存,而应该让内存在内存中断时从缓存中删除内容。这就是soft references的用途。

基于像Googles MapMaker这样的软引用,有一些可用的缓存Java实现,但我不确定这些库带来了多少占用空间,以及你是否更适合在Android上使用自制实现

相关问题