英文:
Memory leak while using LazyRow in jetpack compose
问题
我是Jetpack Compose的新手,可能做了一些愚蠢的事情,但使用懒加载行(lazy row)会导致内存泄漏。请帮忙解决。
(忽略这个,因为stackoverflow要求添加更多文本,我以前从未使用过,不知道该怎么做,随意写了一些东西)
我的代码->
类 MainActivity : ComponentActivity(){
覆盖fun onCreate(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
val playLists = mutableListOf<Playlist>()
val songs = mutableListOf<Songs>()
val artists = mutableListOf<String>()
artists.add("Artist 1")
artists.add("Artist 2")
songs.add(Songs("Song 1","image",artists))
songs.add(Songs("Song 1","image",artists))
songs.add(Songs("Song 1","image",artists))
playLists.add(Playlist(1,R.drawable.cover,"Your Title","Album mix",songs = songs))
playLists.add(Playlist(1,R.drawable.cover,"Your Title","Album day mix",songs = songs))
playLists.add(Playlist(1,R.drawable.cover,"Your Title","Weekend mix",songs = songs))
playLists.add(Playlist(1,R.drawable.cover,"Your Title","New release",songs = songs))
playLists.add(Playlist(1,R.drawable.cover,"Your Title","Top hits",songs = songs))
playLists.add(Playlist(1,R.drawable.cover,"Your Title","Repeat",songs = songs))
setContent({
MusicPlayerTheme(){
Box(
modifier = Modifier
.fillMaxSize()
.background(color = Color.Black)
){
MainScreen(playList = playLists)
}
}
})
}
}
@Composable
fun MainScreen(playList:List<Playlist>){
Column(modifier = Modifier.fillMaxWidth()){
Text(
text = playList [0] .title,
color = Color.White,
style = MaterialTheme.typography.headlineSmall,
fontWeight = FontWeight.Bold,
modifier = Modifier
.fillMaxWidth()
.padding(start = 15.dp,top = 15.dp)
)
LazyRow(
modifier = Modifier
.fillMaxWidth()
.padding(10.dp)
){
items(playList){playList ->
AlbumRows(playList = playList)
}
}
}
}
@Composable
fun AlbumRows(
playList:Playlist
){
Column(modifier = Modifier.padding(5.dp)){
Card(modifier = Modifier.size(180.dp),shape = RectangleShape){
Box(contentAlignment = Alignment.BottomStart){
Image(
painter = painterResource(id = playList.img),
contentDescription = "",
contentScale = ContentScale.Crop
)
Row(
modifier = Modifier
.padding(bottom = 12.dp)
.height(intrinsicSize = IntrinsicSize.Max)
.fillMaxWidth() ,
verticalAlignment = Alignment.CenterVertically
){
Box(
modifier = Modifier
.width(5.dp)
.fillMaxHeight(.8f)
.background(color = MaterialTheme.colorScheme.primary)
)
Text(
text = playList.albumTitle,
color = Color.White,
modifier = Modifier.padding(start = 10.dp),
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.SemiBold
)
}
Box(
modifier = Modifier
.fillMaxWidth()
.height(6.dp)
.background(color = MaterialTheme.colorScheme.primary)
)
}
}
val artists = playList.songs [0] .artistName.toString()
Text(
text = artists.substring(1,artists.length - 1),
style = MaterialTheme.typography.labelMedium,
color = Color.LightGray,
modifier = Modifier
.padding(top = 10.dp)
.alpha(.8f)
)
}
}
英文:
I am new to jetpack compose, might have done some dumb thing, but using lazy row is causing memory leak. Please help out.
(Ignore this because stackoverflow is asking to add more text I have never used that before, so don't know what to do and writing random things)
My code->
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val playLists = mutableListOf<Playlist>()
val songs = mutableListOf<Songs>()
val artists = mutableListOf<String>()
artists.add("Artist 1")
artists.add("Artist 2")
songs.add(Songs("Song 1", "image", artists))
songs.add(Songs("Song 1", "image", artists))
songs.add(Songs("Song 1", "image", artists))
playLists.add(Playlist(1, R.drawable.cover, "Your Title", "Album mix", songs = songs))
playLists.add(Playlist(1, R.drawable.cover, "Your Title", "Album day mix", songs = songs))
playLists.add(Playlist(1, R.drawable.cover, "Your Title", "Weekend mix", songs = songs))
playLists.add(Playlist(1, R.drawable.cover, "Your Title", "New release", songs = songs))
playLists.add(Playlist(1, R.drawable.cover, "Your Title", "Top hits", songs = songs))
playLists.add(Playlist(1, R.drawable.cover, "Your Title", "Repeat", songs = songs))
setContent {
MusicPlayerTheme {
Box(
modifier = Modifier
.fillMaxSize()
.background(color = Color.Black)
) {
MainScreen(playList = playLists)
}
}
}
}
}
@Composable
fun MainScreen(playList: List<Playlist>) {
Column(modifier = Modifier.fillMaxWidth()) {
Text(
text = playList[0].title,
color = Color.White,
style = MaterialTheme.typography.headlineSmall,
fontWeight = FontWeight.Bold,
modifier = Modifier
.fillMaxWidth()
.padding(start = 15.dp, top = 15.dp)
)
LazyRow(
modifier = Modifier
.fillMaxWidth()
.padding(10.dp)
) {
items(playList) { playList ->
AlbumRows(playList = playList)
}
}
}
}
@Composable
fun AlbumRows(
playList: Playlist
) {
Column(modifier = Modifier.padding(5.dp)) {
Card(modifier = Modifier.size(180.dp), shape = RectangleShape) {
Box(contentAlignment = Alignment.BottomStart) {
Image(
painter = painterResource(id = playList.img),
contentDescription = "",
contentScale = ContentScale.Crop
)
Row(
modifier = Modifier
.padding(bottom = 12.dp)
.height(intrinsicSize = IntrinsicSize.Max)
.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Box(
modifier = Modifier
.width(5.dp)
.fillMaxHeight(.8f)
.background(color = MaterialTheme.colorScheme.primary)
)
Text(
text = playList.albumTitle,
color = Color.White,
modifier=Modifier.padding(start = 10.dp),
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.SemiBold
)
}
Box(
modifier = Modifier
.fillMaxWidth()
.height(6.dp)
.background(color = MaterialTheme.colorScheme.primary)
)
}
}
val artists = playList.songs[0].artistName.toString()
Text(
text = artists.substring(1, artists.length - 1),
style = MaterialTheme.typography.labelMedium,
color = Color.LightGray,
modifier = Modifier
.padding(top = 10.dp)
.alpha(.8f)
)
}
}
I thought it was due to reuse of canvas, I removed that but still not worked.
答案1
得分: 0
这似乎与使用ImagePainter有关。在切换到coil进行图像加载后,内存使用量从800 MB降至120MB。
英文:
It seemes related to the use of ImagePainter. After switching to coil for image loading, memory usage went down from 800 MB to 120MB.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论