question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Memory leak in SnapshotStateObserver?

See original GitHub issue

Hi JB team,

I’m trying to get up with a desktop app which updates small part of UI quite frequently (e.g. a timer). As result of app’s normal operation, memory consumption grows relatively quickly. Profiling points to SnapshotStateObserver, which seem doesn’t release some state. However, I don’t see a way how to control that or set a limit.

After just half an hour of work, memory trend is clear: image

On below screenshot you may see Live Bytes and Objects, the max numbers there constantly grow: image

Sample app source:

import androidx.compose.material.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import kotlinx.datetime.Clock
import kotlin.concurrent.fixedRateTimer

fun main() = application {
    remember {
        fixedRateTimer(period = 50) { Holder.refreshClock() }
    }

    Window(onCloseRequest = ::exitApplication) {
        Text(Holder.clock.value)
    }
}

object Holder {
    val clock = mutableStateOf(Clock.System.now().toString())

    fun refreshClock() {
        clock.value = Clock.System.now().toString()
    }
}

OS: Manjaro Linux Compose: 1.1.0 OpenJDK: 11.0.15

Looking forward hearing back from you. Thanks!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
igordmncommented, Nov 10, 2022

Thanks to all for investigating this issue! And especially @pema4 for the fix.

It is fixed, and the fix will be in 1.3.0 (or in 1.2.2, we will decide soon).

3reactions
Ic-kscommented, Oct 14, 2022

I posted the following already at the google issue tracker:

The memory leak is still present in Compose 1.2.0 with Kotlin 1.7.20. I profiled the code below (https://github.com/Ic-ks/compose-memory-leak) for 3 minutes with the Java Flight Recorder (the related .jfr file can be found here: https://issuetracker.google.com/action/issues/223222717/attachments/39334890?download=true). The overall allocated memory was more than 7GB. Heap usage steadily increased to 80 MB. It seems that SnapshotStateObserver does not clean its references properly.

val randomValueFlow = flow { while (true) emit(Math.random() * 100) }
    .onEach { delay(10) }
    .map { it.toInt() }
    .flowOn(Dispatchers.Default)

fun main() = application {
    Window(onCloseRequest = ::exitApplication) {
        val randomValue by randomValueFlow.collectAsState(0L)
        Text(text = randomValue.toString(), fontSize = 20.sp)
    }
}

jfr

PS: .jfr files can be viewed with JDK Mission Control (https://github.com/openjdk/jmc)

Read more comments on GitHub >

github_iconTop Results From Across the Web

LazyColumn memory leak [230168389] - Issue Tracker - Google
This causes a memory leak because the RecomposeScope tries to only forget about object that it is no longer observing if it the...
Read more >
LazyList memory leak in Jetpack Compose - Stack Overflow
I found out that LazyList generates memory leaks. When I scroll down to the bottom of the list memory usage by my app...
Read more >
How to get rid of memory leaks? A practical approach using ...
LeakCanary. LeakCanary is a memory leak detection library for Android, developed by Square. It's a ”plug & play” kind of library where the...
Read more >
Performance Considerations for Memory Leaks: An Android ...
Memory leaks can be found in application code, dependencies, the Android OS — even the JVM. It's difficult to provide an exhaustive list...
Read more >
Everything you need to know about Memory Leaks in Android.
One of the core benefits of Java, or to be more accurate, of the JVM (Java Virtual Machine), is the garbage collector (GC)....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found