Reference

LruCache (Android)

LruCache is Android's in-memory, fixed-size cache that evicts the least recently used entries when it hits its limit. It's the standard way apps cache decoded bitmaps and other objects in RAM, releasing the oldest items first to stay within a memory budget.

Android developmentAndroid

LruCache (Android)

Also known as: lru cache android, memory bitmap cache, android.util.LruCache, android lrucache

LruCache is Android's in-memory, fixed-size cache that evicts the least recently used entries when it hits its limit. It's the standard way apps cache decoded bitmaps and other objects in RAM, releasing the oldest items first to stay within a memory budget.

  • android.util.LruCache is a thread-safe, fixed-size RAM cache that evicts least-recently-used entries.
  • Override sizeOf() to size by bytes; bitmap caches are often budgeted to a fraction of the app heap.
  • It lives in RAM only (not disk) and is typically trimmed in onTrimMemory() under memory pressure.

What LruCache does

android.util.LruCache is a thread-safe cache that keeps a bounded number of strong references to objects and evicts the least recently used entry once it exceeds its configured size. Accessing an entry with get() moves it to the front of the order, so frequently used items survive while stale ones are dropped. It was added in Android 3.1 (API 12) and is also available via the AndroidX/support library.

You size it by overriding sizeOf() to report each entry's cost. For bitmap caches, size is measured in bytes (often via Bitmap.getByteCount()) and the cache budget is set to a fraction of the app's available heap, commonly something like one-eighth of ActivityManager.getMemoryClass(). This keeps the cache useful without pushing the app toward OutOfMemoryError.

Where it fits in app memory and cleanup

LruCache is purely an in-memory (RAM) cache; it does not persist across process death and isn't written to disk. Apps typically pair it with a separate DiskLruCache so a re-decoded image can be reloaded from storage instead of the network, giving a fast RAM tier and a durable disk tier.

Because the in-memory cache competes for RAM, well-behaved apps shrink or clear their LruCache in onTrimMemory() so the OS can reclaim memory under pressure rather than killing the app. From a storage-cleaner perspective, LruCache itself frees instantly when the app is trimmed or closed, while the disk-backed tier is what a cleaner like Cleanor reclaims as cached data.

Related terms

Keep reading the reference.

Act on it

Guides and tools for this topic.