Stackoverflow---关于LruCache类中需要复写的方法 sizeOf() 的含义

 

what happen if I don't override sizeof when using LruCache class

http://stackoverflow.com/questions/25608497/what-happen-if-i-dont-override-sizeof-when-using-lrucache-class

 

I read some of the sample using LruCache to implement a cache mechanism for storing the bitmap image. But I still don't know how to use it even through I have read the documenthttp://developer.android.com/reference/android/util/LruCache.html for it. 
For example, in document, it mentioned "Returns the size of the entry for key and value in user-defined units." in sizeof(). What is the size of entry mean? is it mean the number of entries it allow, e.g return 10 would allow me to have 10 cache object references.

public class LruBitmapCache extends LruCache<String, Bitmap> implements
    ImageCache {
public static int getDefaultLruCacheSize() {
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;

    return cacheSize;
}

public LruBitmapCache() {
    this(getDefaultLruCacheSize());
}

public LruBitmapCache(int sizeInKiloBytes) {
    super(sizeInKiloBytes);
}

@Override
protected int sizeOf(String key, Bitmap value) {
    return getByteCount / 1024;
...

In the above code, why it need to divide 1024, what is the propose for it? 
Also, the constructor LruBitmapCache(int sizeInKiloBytes), why the parameter claim it is size in kilobytes? isn't it should be size in bytes according to the document above? 

Any help would be appreciated, thanks! I am confusing...

shareedit
 

LruCache is used to cache a limited number of values.

But what is this limited number of values?

First option: you want to store x elements in the cache, whatever their size in memory.

In this case, you just create a LruCache with x as the size and you do not override the sizeOfmethod.

For instance:

// cache 1000 values, independently of the String size
LruCache<Integer, String> idToCustomerName = new LruCache<>(1000);

Second option, you want to store elements so that the sum of the size of all elements do not exceed a given amount.

In this case, you create a LruCache with y as the overall size and you do override the sizeOfthat specify the size of one element in the cache.

For instance:

// cache an undefined number of ids so that the length of all the strings 
// do not exceed 100000 characters
LruCache<Integer, String> idToCustomerName = new LruCache<>(100000) {
    @Override
    protected int sizeOf(Integer key, String value) {
       return value.length();
    }
};

To answer your questions about the code, the unit used in the cache does not really matter as long as the maxSize variable and the sizeOf are the same unit.

In your example, the internal unit of the cache is kilobytes, so that's why you see /1024 and /8 in the code, which matches the getByteCount / 1024; in the sizeOf method.

shareedit
 
    
thanks. I get your mean that as long as the unit for maxSize and sizeOf are the same, everything goes correctly. But in your explanation on the second option for the case when i care about the total size of sum of all elements, I have still some confusion. In your code, sizeOf return value.length(). Is it main that if I have 4 items in the LruCache, their size are different, their are 100,200,300,400. Then, the total is 1000. Given that I set maxSize to 100000, e.g LruCache<>(100000). Then, all 4 items are able to store in the cache, right? –  bufferoverflow76 Sep 1 '14 at 16:39
    
Exactly. Now if you have 50000, then 30000 and then 40000, it is too much (sum is 110000) so the least recently used value(s) will be evicted from the cache to make room for the new one. –  Jean Logeart Sep 1 '14 at 18:20
posted @ 2015-05-27 15:24  聪明阿甘  阅读(844)  评论(0编辑  收藏  举报