【发布时间】:2022-07-04 17:24:23
【问题描述】:
我发现 LazyList 会产生内存泄漏。当我向下滚动到列表底部时,我的应用程序的内存使用量增加了约 3MB。向上滚动后,使用量也增加了~3MB。内存使用量一直在增加滚动列表。如何解决? Issue 在 Google 问题跟踪器上
P.S. 有时内存使用量会下降 50-100mb。在我的测试项目(下面的代码)中,使用量增加到 150mb。在我的其他更复杂的项目中,它会增长到 200-250mb。从理论上讲,它可能会导致 OutOfMemoryException。
例子:
LazyVerticalGrid(columns = GridCells.Fixed(2)) {
item(span = { GridItemSpan(2) }) {
LargeTopAppBar(title = {
Text(text = "Explore new")
})
}
items(50) {
Box(modifier = Modifier
.fillMaxWidth()
.height(250.dp))
}
}
【问题讨论】:
-
您是否尝试过运行带有 r8 优化的发行版?如果你能用这么简单的代码重现它,我建议你reporting it。别忘了提一下使用过的compose版本,完美上传样例项目重现问题。
-
Google IO 2022 之后的一些想法:尝试使用“Key”参数 - items(count: Int, key: ((index: Int) -> Any)? = null)
-
@PylypDukhov 是的,当然,我使用 r8 优化。也报告了这个错误
-
@brucemax 不幸的是,密钥并没有阻止内存泄漏。
-
LazyVerticalGrid 目前还不稳定,最好使用 LazyColumn 和 Row 里面
标签: android android-jetpack-compose android-jetpack-compose-list java-memory-leaks