Skip to content

Instantly share code, notes, and snippets.

@kibotu
Last active June 3, 2022 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kibotu/e0a6a0af8ea0eac3932bccf60d2a1edf to your computer and use it in GitHub Desktop.
Save kibotu/e0a6a0af8ea0eac3932bccf60d2a1edf to your computer and use it in GitHub Desktop.
LruBitmapCache
import android.graphics.Bitmap
import androidx.collection.LruCache
val runtimeMemoryChunkInBytes: Int
get() = (Runtime.getRuntime().maxMemory() / 8).toInt()
/**
* https://developer.android.com/topic/performance/graphics/cache-bitmap
*/
class LruBitmapCache : LruCache<String, Bitmap>(runtimeMemoryChunkInBytes) {
init {
Log.i("maxSize=${maxSize()}")
}
override fun entryRemoved(evicted: Boolean, key: String, oldValue: Bitmap, newValue: Bitmap?) {
super.entryRemoved(evicted, key, oldValue, newValue)
if (evicted && !oldValue.isRecycled)
oldValue.recycle()
}
override fun sizeOf(key: String, value: Bitmap): Int = value.allocationByteCount
}
@akdogan
Copy link

akdogan commented Jun 3, 2022

line 19 should be:
if (evicted && !oldValue.isRecycled)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment