nowinandroid icon indicating copy to clipboard operation
nowinandroid copied to clipboard

[Bug]: Compose-related memory leaks

Open brian-livefront opened this issue 1 year ago • 3 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Is there a StackOverflow question about this issue?

  • [X] I have searched StackOverflow

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

  • Open the app.
  • Navigate to the search screen.
  • Enter text in the search field.
  • Exit the search screen.
  • Open the memory profiler, attempt to force a garbage collection several times, and then collect a heap dump.
  • Search for SearchViewModel in the heap.

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

  • [X] I agree to follow this project's Code of Conduct

brian-livefront avatar May 16 '24 17:05 brian-livefront

Hello,

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

  • Navigate to Interests.
  • Open one Topic.
  • Go back to Interests.
  • Return to the Home screen.
  • Open a profiler, record a dump, and search for TopicViewModel.

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

sl-ahmedabdellaoui avatar Jul 26 '24 14:07 sl-ahmedabdellaoui

yes I have encountered the same issue too

carterinchina avatar Aug 08 '24 00:08 carterinchina