From 1867487b94ec76ab54d561cab7c504cf10395ec9 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 10 Jan 2023 22:35:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/updateLog.md | 1 + .../main/java/io/legado/app/help/CacheManager.kt | 13 ++++++++++++- .../java/io/legado/app/utils/StringExtensions.kt | 9 ++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 5c80229b4..250c4e30a 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -14,6 +14,7 @@ **2023/01/10** * 其他一些有分组的地方也支持独立排序 +* 优化搜索,优化缓存,尝试解决部分书源占用内存过大导致OOM * 修复中文AndroidZipFile不能读取非ASCII字符文件名 by ag2s20150909 * 其它一些优化 diff --git a/app/src/main/java/io/legado/app/help/CacheManager.kt b/app/src/main/java/io/legado/app/help/CacheManager.kt index 90309071b..33073bbb8 100644 --- a/app/src/main/java/io/legado/app/help/CacheManager.kt +++ b/app/src/main/java/io/legado/app/help/CacheManager.kt @@ -5,12 +5,23 @@ import io.legado.app.data.appDb import io.legado.app.data.entities.Cache import io.legado.app.model.analyzeRule.QueryTTF import io.legado.app.utils.ACache +import io.legado.app.utils.memorySize @Suppress("unused") object CacheManager { private val queryTTFMap = hashMapOf>() - private val memoryLruCache = object : LruCache(100) {} + + /** + * 最多只缓存50M的数据,防止OOM + */ + private val memoryLruCache = object : LruCache(1024 * 1024 * 50) { + + override fun sizeOf(key: String, value: String): Int { + return value.memorySize() + } + + } /** * saveTime 单位为秒 diff --git a/app/src/main/java/io/legado/app/utils/StringExtensions.kt b/app/src/main/java/io/legado/app/utils/StringExtensions.kt index 3780e9ba8..74d7eac7a 100644 --- a/app/src/main/java/io/legado/app/utils/StringExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/StringExtensions.kt @@ -6,7 +6,6 @@ import android.icu.text.Collator import android.icu.util.ULocale import android.net.Uri import android.text.Editable -import cn.hutool.core.lang.Validator import io.legado.app.constant.AppPattern.dataUriRegex import java.io.File import java.util.* @@ -90,6 +89,14 @@ fun String.cnCompare(other: String): Int { } } +/** + * 字符串所占内存大小 + */ +fun String?.memorySize(): Int { + this ?: return 0 + return 40 + 2 * length +} + /** * 将字符串拆分为单个字符,包含emoji */