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.
In the above code, why it need to divide 1024, what is the propose for it? | |||
add a comment
|
LruCache is used to cache a limited number of values. But what is this limited number of values?First option: you want to store In this case, you just create a For instance:
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 For instance:
To answer your questions about the code, the unit used in the cache does not really matter as long as the In your example, the internal unit of the cache is kilobytes, so that's why you see | |||
maxSize
andsizeOf
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
returnvalue.length()
. Is it main that if I have 4 items in theLruCache
, their size are different, their are 100,200,300,400. Then, the total is 1000. Given that I setmaxSize
to 100000, e.gLruCache<>(100000)
. Then, all 4 items are able to store in the cache, right? – bufferoverflow76 Sep 1 '14 at 16:39