导出使用单线程,导出占用大量内存,并行容易OOM

pull/1977/head
kunfei 2 years ago
parent 72caa55f59
commit 982ced12e4
  1. 4
      app/src/main/assets/updateLog.md
  2. 23
      app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt

@ -11,6 +11,10 @@
* 正文出现缺字漏字、内容缺失、排版错乱等情况,有可能是净化规则或简繁转换出现问题。 * 正文出现缺字漏字、内容缺失、排版错乱等情况,有可能是净化规则或简繁转换出现问题。
* 漫画源看书显示乱码,**阅读与其他软件的源并不通用**,请导入阅读的支持的漫画源! * 漫画源看书显示乱码,**阅读与其他软件的源并不通用**,请导入阅读的支持的漫画源!
**2022/06/14**
* 导出使用单线程,导出占用大量内存,并行容易OOM
**2022/06/10** **2022/06/10**
* 更新cronet: 102.0.5005.125 * 更新cronet: 102.0.5005.125

@ -24,7 +24,10 @@ import io.legado.app.help.ContentProcessor
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import me.ag2s.epublib.domain.* import me.ag2s.epublib.domain.*
import me.ag2s.epublib.epub.EpubWriter import me.ag2s.epublib.epub.EpubWriter
import me.ag2s.epublib.util.ResourceUtil import me.ag2s.epublib.util.ResourceUtil
@ -40,6 +43,10 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
val upAdapterLiveData = MutableLiveData<String>() val upAdapterLiveData = MutableLiveData<String>()
val exportProgress = ConcurrentHashMap<String, Int>() val exportProgress = ConcurrentHashMap<String, Int>()
val exportMsg = ConcurrentHashMap<String, String>() val exportMsg = ConcurrentHashMap<String, String>()
private val mutex = Mutex()
@Volatile
private var exportNumber = 0
private fun getExportFileName(book: Book): String { private fun getExportFileName(book: Book): String {
val jsStr = AppConfig.bookExportFileName val jsStr = AppConfig.bookExportFileName
@ -62,6 +69,12 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
exportMsg.remove(book.bookUrl) exportMsg.remove(book.bookUrl)
upAdapterLiveData.postValue(book.bookUrl) upAdapterLiveData.postValue(book.bookUrl)
execute { execute {
mutex.withLock {
while (exportNumber > 0) {
delay(1000)
}
exportNumber++
}
if (path.isContentScheme()) { if (path.isContentScheme()) {
val uri = Uri.parse(path) val uri = Uri.parse(path)
val doc = DocumentFile.fromTreeUri(context, uri) val doc = DocumentFile.fromTreeUri(context, uri)
@ -79,6 +92,8 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
exportProgress.remove(book.bookUrl) exportProgress.remove(book.bookUrl)
exportMsg[book.bookUrl] = context.getString(R.string.export_success) exportMsg[book.bookUrl] = context.getString(R.string.export_success)
upAdapterLiveData.postValue(book.bookUrl) upAdapterLiveData.postValue(book.bookUrl)
}.onFinally {
exportNumber--
} }
} }
@ -202,6 +217,12 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
exportMsg.remove(book.bookUrl) exportMsg.remove(book.bookUrl)
upAdapterLiveData.postValue(book.bookUrl) upAdapterLiveData.postValue(book.bookUrl)
execute { execute {
mutex.withLock {
while (exportNumber > 0) {
delay(1000)
}
exportNumber++
}
if (path.isContentScheme()) { if (path.isContentScheme()) {
val uri = Uri.parse(path) val uri = Uri.parse(path)
val doc = DocumentFile.fromTreeUri(context, uri) val doc = DocumentFile.fromTreeUri(context, uri)
@ -219,6 +240,8 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
exportProgress.remove(book.bookUrl) exportProgress.remove(book.bookUrl)
exportMsg[book.bookUrl] = context.getString(R.string.export_success) exportMsg[book.bookUrl] = context.getString(R.string.export_success)
upAdapterLiveData.postValue(book.bookUrl) upAdapterLiveData.postValue(book.bookUrl)
}.onFinally {
exportNumber--
} }
} }

Loading…
Cancel
Save