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 using "LifecycleExt.kt"

See original GitHub issue

Hello Arkadiy, thank you very much for such a great library!

It seems to me there is a memory leak in this file: https://github.com/arkivanov/MVIKotlin/blob/bad123550ff7e451708280efbc4394ffdc347107/mvikotlin/src/commonMain/kotlin/com/arkivanov/mvikotlin/core/lifecycle/LifecycleExt.kt#L49

Probably for all these methods: Lifecycle.doOnResumePause, Lifecycle.doOnPause, Lifecycle.doOnStartStop, Lifecycle.doOnStop.

As soon as an anonymous callback instance is created, there is no chance to remove this object until onDestroy event. And if someone uses these methods in onStart then on each call a new callback will be added to LifecycleRegistry.

As a variant to fix that, I can suggest returning Disposable to let a client to remove/unsubscribe from LifecycleRegistry in this way:

inline fun Lifecycle.doOnResumePause(crossinline onResume: () -> Unit, crossinline onPause: () -> Unit): Disposable {
    val callbacks = object : DefaultLifecycleCallbacks {
        override fun onResume() {
            onResume.invoke()
        }

        override fun onPause() {
            onPause.invoke()
        }
    }
    subscribe(callbacks)
    return CancellableDisposable { unsubscribe(callbacks) }
}

// Then from the client side we can call `disposable?.dispose()` to remove added callback.

At the same time there is no such issue with Lifecycle.doOnDestroy and Lifecycle.doOnCreateDestroy (at least on Android platform) because Android LifecycleRegistry removes a callback automatically after onDestroy event.

@arkivanov does it make sense?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
arkivanovcommented, Jun 14, 2021

Will be delivered in version 2.1.0

0reactions
arkivanovcommented, Aug 2, 2021

Fixed via #259

Read more comments on GitHub >

github_iconTop Results From Across the Web

ViewLifecycleLazy and other ways to avoid View memory ...
Until now, the number one cause of memory leaks when using Fragments remains the same: not properly clearing all direct and indirect View...
Read more >
Getting a memory leak after calling lifecycle.addObserver
Event.ON_RESUME), You can remove the listener or receiver in onPause lifecycle method and then again intialize in onResume lifecycle method.
Read more >
Android memory leak - Bugfender
This article will explain how to minimize this risk through effective lifecycle management — a crucial but often overlooked part of the ...
Read more >
Memory leak with 1.5.0-RC when building with Gradle
We recently upgraded Spring Boot to Kotlin 1.5.0-RC and, since the upgrade, have observed a memory leak in Gradle's daemon. Downgrading to Kotlin...
Read more >
Memory Leaks in Android | Kodeco, the new raywenderlich.com
kt lets the user press a button when he or she thinks the count is over. Finding a Leak Using The Android Profiler....
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