From 982ced12e43cde7e3dae2cdf2ad910f6ee9031fa Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 14 Jun 2022 20:54:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=87=BA=E4=BD=BF=E7=94=A8=E5=8D=95?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B,=E5=AF=BC=E5=87=BA=E5=8D=A0=E7=94=A8?= =?UTF-8?q?=E5=A4=A7=E9=87=8F=E5=86=85=E5=AD=98,=E5=B9=B6=E8=A1=8C?= =?UTF-8?q?=E5=AE=B9=E6=98=93OOM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/updateLog.md | 4 ++++ .../app/ui/book/cache/CacheViewModel.kt | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index c2b36a80e..1683d9fee 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -11,6 +11,10 @@ * 正文出现缺字漏字、内容缺失、排版错乱等情况,有可能是净化规则或简繁转换出现问题。 * 漫画源看书显示乱码,**阅读与其他软件的源并不通用**,请导入阅读的支持的漫画源! +**2022/06/14** + +* 导出使用单线程,导出占用大量内存,并行容易OOM + **2022/06/10** * 更新cronet: 102.0.5005.125 diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt index d08e0c956..7b66ff6c7 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt @@ -24,7 +24,10 @@ import io.legado.app.help.ContentProcessor import io.legado.app.help.config.AppConfig import io.legado.app.utils.* import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.delay import kotlinx.coroutines.ensureActive +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import me.ag2s.epublib.domain.* import me.ag2s.epublib.epub.EpubWriter import me.ag2s.epublib.util.ResourceUtil @@ -40,6 +43,10 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { val upAdapterLiveData = MutableLiveData() val exportProgress = ConcurrentHashMap() val exportMsg = ConcurrentHashMap() + private val mutex = Mutex() + + @Volatile + private var exportNumber = 0 private fun getExportFileName(book: Book): String { val jsStr = AppConfig.bookExportFileName @@ -62,6 +69,12 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { exportMsg.remove(book.bookUrl) upAdapterLiveData.postValue(book.bookUrl) execute { + mutex.withLock { + while (exportNumber > 0) { + delay(1000) + } + exportNumber++ + } if (path.isContentScheme()) { val uri = Uri.parse(path) val doc = DocumentFile.fromTreeUri(context, uri) @@ -79,6 +92,8 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { exportProgress.remove(book.bookUrl) exportMsg[book.bookUrl] = context.getString(R.string.export_success) upAdapterLiveData.postValue(book.bookUrl) + }.onFinally { + exportNumber-- } } @@ -202,6 +217,12 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { exportMsg.remove(book.bookUrl) upAdapterLiveData.postValue(book.bookUrl) execute { + mutex.withLock { + while (exportNumber > 0) { + delay(1000) + } + exportNumber++ + } if (path.isContentScheme()) { val uri = Uri.parse(path) val doc = DocumentFile.fromTreeUri(context, uri) @@ -219,6 +240,8 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { exportProgress.remove(book.bookUrl) exportMsg[book.bookUrl] = context.getString(R.string.export_success) upAdapterLiveData.postValue(book.bookUrl) + }.onFinally { + exportNumber-- } }