Initialising the cache

suggest change

The LruCache will store all the added resources (values) for fast access until it reaches a memory limit, in which case it drops the least recently used resource (value) to store the new one.

To initialise the LruCache you need to provide a maximum memory value. This value depends on your application requirements and on how critical the resource is to keep a smooth app usage. A recommended value for an image gallery, for example, would be 1/8 of your maximum available memory.

Also note that the Lru Cache works on a key-value basis. In the following example, the key is a String and the value is a Bitmap:

int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
int cacheSize = maxMemory / 8;

LruCache<String, Bitmap> memoryCache = new LruCache<String, Bitmap>(cacheSize) {
    protected int sizeOf(String key, Bitmap bitmap) {
        // cacheSize is in KB because maxMemory was divided by 1024
        return bitmap.getByteCount() / 1024;
    }
};

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents