[Android] Using LruCache to cache Bitmaps and limit memory usage

Bitmaps are very bulk objects, loading them from the Internet or local storage could be expensive and “choke” the UI. You want to  keep them in memory so that they could be loaded instantly, however you also want to discard them as soon as they are not useful. Here comes the LruCache.

What is LruCache?

LruCache is a util introduced in API 12, which is an implementaion of the Least Recently Used cache. In short, that is a caching strategy which the “oldest” entries are discarded when overflow occur. It is very useful to cache Bitmaps. The default implementation allows you to limits the number of items in the cache. Here’s how to slightly modify the LruCache to limit the memory usage of the cache.

How to use it?

Well, actually it act just like a HashMap. Continue reading “[Android] Using LruCache to cache Bitmaps and limit memory usage”