image: fixed 10MB memory and 200MB disk caches, drop largeHeap
- Commit
109b2348ca523cfb0a977fa3fa5f596d2f33ba11- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-26 19:28 UTC (1 month ago)
- Parents
1aa7468
Diff
library/image/src/main/java/com/vayunmathur/library/image/DiskCache.kt
diff --git a/library/image/src/main/java/com/vayunmathur/library/image/DiskCache.kt b/library/image/src/main/java/com/vayunmathur/library/image/DiskCache.ktindex e8f3e7239d..519f872bb9 100644--- a/library/image/src/main/java/com/vayunmathur/library/image/DiskCache.kt+++ b/library/image/src/main/java/com/vayunmathur/library/image/DiskCache.kt@@ -11,8 +11,7 @@ import kotlin.concurrent.withLock * - Key is hashed via SHA-256 to form a filename. * - Raw bytes are stored (encoded image bytes, e.g. JPEG/PNG). * - Eviction: oldest lastModified first when over size limit.- *- * Mirrors Coil diskCache { directory(cacheDir/image_cache).maxSizePercent(0.05) } shape.+ * - Fixed at 200 MB. * * Locking is striped per key rather than global: during a fast scroll, dozens of * unrelated thumbnails are read and written at once, and a single lock made them@@ -187,6 +186,7 @@ class DiskCache internal constructor( private companion object { const val STRIPE_COUNT = 16+ const val MAX_BYTES = 200L * 1024 * 1024 /** Marks in-progress writes so they are never counted or evicted. */ const val TMP_MARKER = ".tmp_"@@ -201,39 +201,15 @@ class DiskCache internal constructor( class Builder { private var directory: File? = null- private var maxSizePercent: Double = 0.02- private var maxSizeBytes: Long? = null fun directory(dir: File): Builder { directory = dir return this } - fun maxSizePercent(percent: Double): Builder {- maxSizePercent = percent- return this- }-- fun maxSizePercent(percent: Float): Builder = maxSizePercent(percent.toDouble())-- /** Optional fixed size */- fun maxSizeBytes(bytes: Long): Builder {- maxSizeBytes = bytes- return this- }- fun build(): DiskCache { val dir = directory ?: throw IllegalStateException("DiskCache directory not set")- val maxBytes = maxSizeBytes ?: run {- try {- val stat = android.os.StatFs(dir.absolutePath)- val total = stat.blockCountLong * stat.blockSizeLong- (total * maxSizePercent).toLong().coerceAtLeast(10L * 1024 * 1024)- } catch (_: Exception) {- 50L * 1024 * 1024- }- }- return DiskCache(dir, maxBytes)+ return DiskCache(dir, MAX_BYTES) } } }
library/image/src/main/java/com/vayunmathur/library/image/ImageLoader.kt
diff --git a/library/image/src/main/java/com/vayunmathur/library/image/ImageLoader.kt b/library/image/src/main/java/com/vayunmathur/library/image/ImageLoader.ktindex 698852a1e5..8934888db5 100644--- a/library/image/src/main/java/com/vayunmathur/library/image/ImageLoader.kt+++ b/library/image/src/main/java/com/vayunmathur/library/image/ImageLoader.kt@@ -401,10 +401,10 @@ class ImageLoader private constructor( } fun build(): ImageLoader {- val mem = memoryCacheInstance ?: MemoryCache.Builder(context.applicationContext).build()+ val mem = memoryCacheInstance ?: MemoryCache.Builder().build() val disk = diskCacheInstance ?: try { val dir = context.applicationContext.cacheDir.resolve("image_cache")- DiskCache.Builder().directory(dir).maxSizePercent(0.05).build()+ DiskCache.Builder().directory(dir).build() } catch (_: Exception) { null } val defaultFetchers = listOf(
library/image/src/main/java/com/vayunmathur/library/image/MemoryCache.kt
diff --git a/library/image/src/main/java/com/vayunmathur/library/image/MemoryCache.kt b/library/image/src/main/java/com/vayunmathur/library/image/MemoryCache.ktindex 4c243e5d38..d7bcef1351 100644--- a/library/image/src/main/java/com/vayunmathur/library/image/MemoryCache.kt+++ b/library/image/src/main/java/com/vayunmathur/library/image/MemoryCache.kt@@ -1,14 +1,11 @@ package com.vayunmathur.library.image -import android.content.Context-import android.content.pm.ApplicationInfo import android.graphics.Bitmap import android.util.LruCache-import com.vayunmathur.library.image.util.maxMemoryBytes /** * Simple LRU memory cache counting [Bitmap.byteCount].- * Mirrors coil.memory.MemoryCache.Builder(maxSizePercent) API shape.+ * Fixed at 10 MB — deliberately not sized off the device heap. */ class MemoryCache internal constructor( private val lru: LruCache<String, Bitmap>,@@ -23,37 +20,13 @@ class MemoryCache internal constructor( fun clear() = lru.evictAll() - class Builder(private val context: Context) {- private var maxSizePercent: Double = 0.25- private var maxSizeBytes: Int? = null-- fun maxSizePercent(percent: Double): Builder {- maxSizePercent = percent- return this- }-- /** Alias for Coil which exposed Float overload */- fun maxSizePercent(percent: Float): Builder = maxSizePercent(percent.toDouble())-- fun maxSizeBytes(bytes: Int): Builder {- maxSizeBytes = bytes- return this- }+ companion object {+ const val MAX_BYTES = 10 * 1024 * 1024+ } + class Builder { fun build(): MemoryCache {- val maxBytes = maxSizeBytes ?: run {- val am = context.getSystemService(Context.ACTIVITY_SERVICE) as android.app.ActivityManager- // An app that asked for android:largeHeap gets the large heap, so- // sizing off memoryClass gave it a cache measured for a heap it- // isn't using — small enough that a single full-resolution decode- // could evict everything else.- val isLargeHeap = (context.applicationInfo.flags and ApplicationInfo.FLAG_LARGE_HEAP) != 0- val heapMb = if (isLargeHeap) am.largeMemoryClass else am.memoryClass- val memClass = heapMb * 1024 * 1024- (memClass * maxSizePercent).toInt().coerceAtLeast(4 * 1024 * 1024)- }-- val cache = object : LruCache<String, Bitmap>(maxBytes) {+ val cache = object : LruCache<String, Bitmap>(MAX_BYTES) { override fun sizeOf(key: String, value: Bitmap): Int { return try { value.byteCount
library/image/src/main/java/com/vayunmathur/library/image/util/PercentSizing.kt
diff --git a/library/image/src/main/java/com/vayunmathur/library/image/util/PercentSizing.kt b/library/image/src/main/java/com/vayunmathur/library/image/util/PercentSizing.ktindex 26448597cd..58f65de9e8 100644--- a/library/image/src/main/java/com/vayunmathur/library/image/util/PercentSizing.kt+++ b/library/image/src/main/java/com/vayunmathur/library/image/util/PercentSizing.kt@@ -1,36 +1,7 @@ package com.vayunmathur.library.image.util -import android.app.ActivityManager-import android.content.Context-import android.os.StatFs import java.io.File -private const val BYTES_PER_KB = 1024L-private const val BYTES_PER_MB = BYTES_PER_KB * BYTES_PER_KB-private const val MIN_MEMORY_CACHE_BYTES = 4 * 1024 * 1024-private const val MIN_DISK_CACHE_BYTES = 10L * 1024 * 1024-private const val FALLBACK_DISK_CACHE_BYTES = 50L * 1024 * 1024--fun Context.maxMemoryBytes(): Long {- val am = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager- return am.memoryClass * BYTES_PER_MB-}--fun memoryCacheMaxBytes(context: Context, percent: Double): Int {- val max = context.maxMemoryBytes()- return (max * percent).toInt().coerceAtLeast(MIN_MEMORY_CACHE_BYTES)-}--fun diskCacheMaxBytes(dir: File, percent: Double): Long {- return try {- val stat = StatFs(dir.absolutePath)- val total = stat.blockCountLong * stat.blockSizeLong- (total * percent).toLong().coerceAtLeast(MIN_DISK_CACHE_BYTES)- } catch (_: Exception) {- FALLBACK_DISK_CACHE_BYTES // 50MB fallback- }-}- fun File.ensureExists(): File { if (!exists()) mkdirs() return this
logviewer/src/main/AndroidManifest.xml
diff --git a/logviewer/src/main/AndroidManifest.xml b/logviewer/src/main/AndroidManifest.xmlindex 2e423b6413..5c8cb456ac 100644--- a/logviewer/src/main/AndroidManifest.xml+++ b/logviewer/src/main/AndroidManifest.xml@@ -43,10 +43,7 @@ android:description="@string/permission_show_error_report_desc" android:protectionLevel="signature|preinstalled" /> - <!-- largeHeap because a full logcat dump is held in memory as one string plus one string per- line; this is inherited from the app being replaced and is not optional.-- usesNonSdkApi is what makes platform/HiddenFrameworkApi.kt actually work, and without it+ <!-- usesNonSdkApi is what makes platform/HiddenFrameworkApi.kt actually work, and without it that file is dead code. ApplicationInfo.isAllowedToUseHiddenApis() (framework source, ApplicationInfo.java:2646-2654) reads: if (isSignedWithPlatformKey()) return true;@@ -66,7 +63,6 @@ <application android:icon="@mipmap/ic_launcher" android:label="@string/app_name"- android:largeHeap="true" android:roundIcon="@mipmap/ic_launcher" android:supportsRtl="true" android:theme="@style/Theme.General"
maps/src/main/AndroidManifest.xml
diff --git a/maps/src/main/AndroidManifest.xml b/maps/src/main/AndroidManifest.xmlindex 222233ae1e..dabb679e5a 100644--- a/maps/src/main/AndroidManifest.xml+++ b/maps/src/main/AndroidManifest.xml@@ -43,7 +43,6 @@ <application- android:largeHeap="true" android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:icon="@mipmap/ic_launcher"
photos/src/main/java/com/vayunmathur/photos/util/ImageLoader.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/util/ImageLoader.kt b/photos/src/main/java/com/vayunmathur/photos/util/ImageLoader.ktindex bcc6014d31..85a1d805dc 100644--- a/photos/src/main/java/com/vayunmathur/photos/util/ImageLoader.kt+++ b/photos/src/main/java/com/vayunmathur/photos/util/ImageLoader.kt@@ -37,8 +37,6 @@ import com.vayunmathur.photos.data.Photo import com.vayunmathur.photos.R object ImageLoader {- private const val MEMORY_CACHE_FRACTION = 0.25- private const val DISK_CACHE_FRACTION = 0.05 private const val VIDEO_FRAME_MILLIS = 1000L private const val THUMBNAIL_SIZE = 256 @@ -64,12 +62,11 @@ object ImageLoader { private fun build(context: Context): LibImageLoader = LibImageLoader.Builder(context) .memoryCache {- MemoryCache.Builder(context).maxSizePercent(MEMORY_CACHE_FRACTION).build()+ MemoryCache.Builder().build() } .diskCache { DiskCache.Builder() .directory(context.cacheDir.resolve("image_cache"))- .maxSizePercent(DISK_CACHE_FRACTION) .build() } .respectCacheHeaders(false)