android / nowinandroid

A fully functional Android app built entirely with Kotlin and Jetpack Compose
Apache License 2.0
16.62k stars 3.01k forks source link

[Bug]: Compose-related memory leaks #1446

Open brian-livefront opened 4 months ago

brian-livefront commented 4 months ago

Is there an existing issue for this?

Is there a StackOverflow question about this issue?

What happened?

The SearchViewModel (and likely other ViewModels) appears to leak after navigating to the search screen and back to the main screen. This seems to be related to this Compose issue.

Steps to reproduce

The SearchViewModel will still be present in the heap even though the screen was left and many garbage collections were attempted.

Screenshot 2024-05-16 at 12 50 46 PM

Relevant logcat output

No response

Code of Conduct

sl-ahmedabdellaoui commented 2 months ago

Hello,

I have encountered the same issue with TopicViewModel. Here are the steps to reproduce it:

You'll notice that TopicViewModel is still present. The memory leak appears to be related to a lambda function referencing the ViewModel. Upon further inspection, it seems the ViewModel is retained by the AccessibilityManager as shown below:

Capture d’écran 2024-07-26 à 16 08 12

Is there any update on this issue ?

By the way, even in another Android app using Compose, I noticed the same behavior. ViewModels seem to be kept in memory even after onCleared is called. thank you

carterinchina commented 1 month ago

yes I have encountered the same issue too