LruCache
added in API level 12
A cache that holds strong references to a limited number of values.Each time a value is accessed,it is moved to the headof a queue.When a value is added to a full cache,
the value at the end of that queue is evicted and may become eligible for garbage collection.
By default, the cache size is measured in the number of entries. Override sizeOf(K, V)
to size the cache in different units. For example, this cache is limited to 4MiB of bitmaps:
int cacheSize = 4 * 1024 * 1024; // 4MiB
LruCache<String, Bitmap> bitmapCache = new LruCache<String, Bitmap>(cacheSize) {
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
}
/**
* Returns the size of the entry for {@code key} and {@code value} in
* user-defined units. The default implementation returns 1 so that size
* is the number of entries and max size is the maximum number of entries.
*
* <p>An entry's size must not change while it is in the cache.
*/
protected int sizeOf(K key, V value) {
return 1;
}