# LruCache

# LruCache

Artifact:[androidx.collection:collection](https://developer.android.com/jetpack/androidx/releases/collection)  
[View Source](https://cs.android.com/search?q=file:androidx/collection/LruCache.kt+class:androidx.collection.LruCache)  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/collection/LruCache "View this page in Java")
Common/AllAndroid/JVMNative/C/iOSJavaScript
  - Cmn  

  ```
  open class LruCache<K : Any, V : Any>
  ```

*** ** * ** ***

A cache that holds strong references to a limited number of values. Each time a value is accessed, it is moved to the head of 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.

If your cached values hold resources that need to be explicitly released, override[entryRemoved](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#entryRemoved(kotlin.Boolean,kotlin.Any,kotlin.Any,kotlin.Any)).

If a cache miss should be computed on demand for the corresponding keys, override[create](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#create(kotlin.Any)). This simplifies the calling code, allowing it to assume a value will always be returned, even when there's a cache miss.

By default, the cache size is measured in the number of entries. Override[sizeOf](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#sizeOf(kotlin.Any,kotlin.Any))to size the cache in different units. For example, this cache is limited to 4MiB of bitmaps:  

```kotlin
val cacheSize = 4 * 1024 * 1024; // 4MiB
val bitmapCache = LruCache<String, Bitmap>(cacheSize) {
    override fun sizeOf(key: String, value: Bitmap) = value.byteCount
}
```

This class is thread-safe.

This class does not allow null to be used as a key or value. A return value of null from[get](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#get(kotlin.Any)),[put](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#put(kotlin.Any,kotlin.Any))or[remove](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#remove(kotlin.Any))is unambiguous: the key was not in the cache.

## Summary

|                                                                                                                                                                                                                                                                                          ### Public constructors                                                                                                                                                                                                                                                                                           |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----|
| `<K : `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`, V : `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`> `[LruCache](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#LruCache(kotlin.Int))`(maxSize: @`[IntRange](https://developer.android.com/reference/kotlin/androidx/annotation/IntRange)`(from = 1, to = 2147483647) `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Creates a new[LruCache](https://developer.android.com/reference/kotlin/androidx/collection/LruCache) | Cmn |

|                                              ### Public functions                                              |
|----------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                    | [createCount](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#createCount())`()` Returns the number of times[create](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#create(kotlin.Any))returned a value.                                                                                                                           | Cmn |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                  | [evictAll](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#evictAll())`()` Clear the cache, calling[entryRemoved](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#entryRemoved(kotlin.Boolean,kotlin.Any,kotlin.Any,kotlin.Any))on each removed entry.                                                                              | Cmn |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                    | [evictionCount](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#evictionCount())`()` Returns the number of values that have been evicted.                                                                                                                                                                                                                       | Cmn |
| `operator V?`                                                                                                  | [get](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#get(kotlin.Any))`(key: K)` Returns the value for[key](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#get(kotlin.Any))if it exists in the cache or can be created by[create](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#create(kotlin.Any)). | Cmn |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                    | [hitCount](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#hitCount())`()` Returns the number of times[get](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#get(kotlin.Any))returned a value that was already present in the cache.                                                                                                 | Cmn |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                    | [maxSize](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#maxSize())`()` For caches that do not override[sizeOf](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#sizeOf(kotlin.Any,kotlin.Any)), this returns the maximum number of entries in the cache.                                                                           | Cmn |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                    | [missCount](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#missCount())`()` Returns the number of times[get](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#get(kotlin.Any))returned null or required a new value to be created.                                                                                                  | Cmn |
| `V?`                                                                                                           | [put](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#put(kotlin.Any,kotlin.Any))`(key: K, value: V)` Caches[value](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#put(kotlin.Any,kotlin.Any))for[key](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#put(kotlin.Any,kotlin.Any)).                    | Cmn |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                    | [putCount](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#putCount())`()` Returns the number of times[put](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#put(kotlin.Any,kotlin.Any))was called.                                                                                                                                  | Cmn |
| `V?`                                                                                                           | [remove](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#remove(kotlin.Any))`(key: K)` Removes the entry for[key](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#remove(kotlin.Any))if it exists.                                                                                                                                  | Cmn |
| `open `[Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                           | [resize](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#resize(kotlin.Int))`(maxSize: @`[IntRange](https://developer.android.com/reference/kotlin/androidx/annotation/IntRange)`(from = 1, to = 2147483647) `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Sets the size of the cache.                                        | Cmn |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                    | [size](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#size())`()` For caches that do not override[sizeOf](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#sizeOf(kotlin.Any,kotlin.Any)), this returns the number of entries in the cache.                                                                                         | Cmn |
| [MutableMap](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-map/index.html)`<K, V>` | [snapshot](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#snapshot())`()` Returns a mutable copy of the current contents of the cache, ordered from least recently accessed to most recently accessed.                                                                                                                                                         | Cmn |
| `open `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)                       | [toString](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#toString())`()`                                                                                                                                                                                                                                                                                      | Cmn |
| `open `[Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                           | [trimToSize](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#trimToSize(kotlin.Int))`(maxSize: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Remove the eldest entries until the total of remaining entries is at or below the requested size.                                                                                | Cmn |

|                               ### Protected functions                                |
|--------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----|
| `open V?`                                                                            | [create](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#create(kotlin.Any))`(key: K)` Called after a cache miss to compute a value for the corresponding key.                                                                                                                                                                                                                                           | Cmn |
| `open `[Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [entryRemoved](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#entryRemoved(kotlin.Boolean,kotlin.Any,kotlin.Any,kotlin.Any))`(evicted: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)`, key: K, oldValue: V, newValue: V?)` Called for entries that have been evicted or removed.                                                                                 | Cmn |
| `open `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)   | [sizeOf](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#sizeOf(kotlin.Any,kotlin.Any))`(key: K, value: V)` Returns the size of the entry for[key](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#sizeOf(kotlin.Any,kotlin.Any))and[value](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#sizeOf(kotlin.Any,kotlin.Any))in user-defined units. | Cmn |

## Public constructors

### LruCache

Cmn  

```
<K : Any, V : Any> LruCache(maxSize: @IntRange(from = 1, to = 2147483647) Int)
```

Creates a new[LruCache](https://developer.android.com/reference/kotlin/androidx/collection/LruCache)  

|                                                                                                  Parameters                                                                                                  |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `maxSize: @`[IntRange](https://developer.android.com/reference/kotlin/androidx/annotation/IntRange)`(from = 1, to = 2147483647) `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | for caches that do not override[sizeOf](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#sizeOf(kotlin.Any,kotlin.Any)), this is the maximum number of entries in the cache. For all other caches, this is the maximum sum of the sizes of the entries in this cache. |

## Public functions

### createCount

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
fun createCount(): Int
```

Returns the number of times[create](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#create(kotlin.Any))returned a value.  

### evictAll

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
fun evictAll(): Unit
```

Clear the cache, calling[entryRemoved](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#entryRemoved(kotlin.Boolean,kotlin.Any,kotlin.Any,kotlin.Any))on each removed entry.  

### evictionCount

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
fun evictionCount(): Int
```

Returns the number of values that have been evicted.  

### get

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
operator fun get(key: K): V?
```

Returns the value for[key](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#get(kotlin.Any))if it exists in the cache or can be created by[create](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#create(kotlin.Any)). If a value was returned, it is moved to the head of the queue. This returns`null`if a value is not cached and cannot be created.  

### hitCount

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
fun hitCount(): Int
```

Returns the number of times[get](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#get(kotlin.Any))returned a value that was already present in the cache.  

### maxSize

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
fun maxSize(): Int
```

For caches that do not override[sizeOf](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#sizeOf(kotlin.Any,kotlin.Any)), this returns the maximum number of entries in the cache. For all other caches, this returns the maximum sum of the sizes of the entries in this cache.  

### missCount

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
fun missCount(): Int
```

Returns the number of times[get](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#get(kotlin.Any))returned null or required a new value to be created.  

### put

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
fun put(key: K, value: V): V?
```

Caches[value](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#put(kotlin.Any,kotlin.Any))for[key](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#put(kotlin.Any,kotlin.Any)). The value is moved to the head of the queue.  

| Returns |
|---------|--------------------------------------------------------------------------------------------------------------------------------------------|
| `V?`    | the previous value mapped by[key](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#put(kotlin.Any,kotlin.Any)). |

### putCount

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
fun putCount(): Int
```

Returns the number of times[put](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#put(kotlin.Any,kotlin.Any))was called.  

### remove

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
fun remove(key: K): V?
```

Removes the entry for[key](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#remove(kotlin.Any))if it exists.  

| Returns |
|---------|------------------------------------------------------------------------------------------------------------------------------------|
| `V?`    | the previous value mapped by[key](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#remove(kotlin.Any)). |

### resize

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
open fun resize(maxSize: @IntRange(from = 1, to = 2147483647) Int): Unit
```

Sets the size of the cache.  

|                                                                                                  Parameters                                                                                                  |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------|
| `maxSize: @`[IntRange](https://developer.android.com/reference/kotlin/androidx/annotation/IntRange)`(from = 1, to = 2147483647) `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The new maximum size. |

### size

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
fun size(): Int
```

For caches that do not override[sizeOf](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#sizeOf(kotlin.Any,kotlin.Any)), this returns the number of entries in the cache. For all other caches, this returns the sum of the sizes of the entries in this cache.  

### snapshot

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
fun snapshot(): MutableMap<K, V>
```

Returns a mutable copy of the current contents of the cache, ordered from least recently accessed to most recently accessed.  

### toString

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
open fun toString(): String
```  

### trimToSize

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
open fun trimToSize(maxSize: Int): Unit
```

Remove the eldest entries until the total of remaining entries is at or below the requested size.  

|                                       Parameters                                       |
|----------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------|
| `maxSize: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the maximum size of the cache before returning. May be -1 to evict even 0-sized elements. |

## Protected functions

### create

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
protected open fun create(key: K): V?
```

Called after a cache miss to compute a value for the corresponding key. Returns the computed value or null if no value can be computed. The default implementation returns null.

The method is called without synchronization: other threads may access the cache while this method is executing.

If a value for[key](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#create(kotlin.Any))exists in the cache when this method returns, the created value will be released with[entryRemoved](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#entryRemoved(kotlin.Boolean,kotlin.Any,kotlin.Any,kotlin.Any))and discarded. This can occur when multiple threads request the same key at the same time (causing multiple values to be created), or when one thread calls[put](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#put(kotlin.Any,kotlin.Any))while another is creating a value for the same key.  

### entryRemoved

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
protected open fun entryRemoved(evicted: Boolean, key: K, oldValue: V, newValue: V?): Unit
```

Called for entries that have been evicted or removed. This method is invoked when a value is evicted to make space, removed by a call to[remove](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#remove(kotlin.Any)), or replaced by a call to[put](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#put(kotlin.Any,kotlin.Any)). The default implementation does nothing.

The method is called without synchronization: other threads may access the cache while this method is executing.  

|                                           Parameters                                           |
|------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `evicted: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | `true`if the entry is being removed to make space,`false`if the removal was caused by a[put](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#put(kotlin.Any,kotlin.Any))or[remove](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#remove(kotlin.Any)).                                                                                                                                                                               |
| `key: K`                                                                                       | key of the entry that was evicted or removed.                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| `oldValue: V`                                                                                  | the original value of the entry that was evicted removed.                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `newValue: V?`                                                                                 | the new value for[key](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#entryRemoved(kotlin.Boolean,kotlin.Any,kotlin.Any,kotlin.Any)), if it exists. If non-null, this removal was caused by a[put](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#put(kotlin.Any,kotlin.Any)). Otherwise it was caused by an eviction or a[remove](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#remove(kotlin.Any)). |

### sizeOf

Cmn  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/collection#1.0.0)  

```
protected open fun sizeOf(key: K, value: V): Int
```

Returns the size of the entry for[key](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#sizeOf(kotlin.Any,kotlin.Any))and[value](https://developer.android.com/reference/kotlin/androidx/collection/LruCache#sizeOf(kotlin.Any,kotlin.Any))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.

An entry's size must not change while it is in the cache.