Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detecting ViewModel leaks? #1731

Closed
pyricau opened this issue Jan 24, 2020 · 2 comments · Fixed by #1732
Closed

Detecting ViewModel leaks? #1731

pyricau opened this issue Jan 24, 2020 · 2 comments · Fixed by #1732
Milestone

Comments

@pyricau
Copy link
Member

pyricau commented Jan 24, 2020

ViewModels have a lifecycle (they get "cleared), so I wonder if we can automate the detection of view models leaking or at least provide some utilities.

pyricau added a commit that referenced this issue Jan 30, 2020
AndroidXFragmentDestroyWatcher adds a spy view model in every ViewModelStoreOwner instance (ie FragmentActivity and Fragment). ViewModelClearedWatcher
is a view model that holds on to the map of view models backing its store. When ViewModelClearedWatcher receives
the onCleared() callback, it adds each live view model from the store to the ObjectWatcher.

Other changes:

* Renamed FragmentLeakTest to LifecycleLeaksTest.
* Added tests for activity leaks and view model leaks.
* Refactored leak tests for readability.

Fixes #1731
pyricau added a commit that referenced this issue Jan 30, 2020
AndroidXFragmentDestroyWatcher adds a spy view model in every ViewModelStoreOwner instance (ie FragmentActivity and Fragment). ViewModelClearedWatcher
is a view model that holds on to the map of view models backing its store. When ViewModelClearedWatcher receives
the onCleared() callback, it adds each live view model from the store to the ObjectWatcher.

Other changes:

* Renamed FragmentLeakTest to LifecycleLeaksTest.
* Added tests for activity leaks and view model leaks.
* Refactored leak tests for readability.

Fixes #1731
pyricau added a commit that referenced this issue Jan 30, 2020
AndroidXFragmentDestroyWatcher adds a spy view model in every ViewModelStoreOwner instance (ie FragmentActivity and Fragment). ViewModelClearedWatcher
is a view model that holds on to the map of view models backing its store. When ViewModelClearedWatcher receives
the onCleared() callback, it adds each live view model from the store to the ObjectWatcher.

Other changes:

* Renamed FragmentLeakTest to LifecycleLeaksTest.
* Added tests for activity leaks and view model leaks.
* Refactored leak tests for readability.

Fixes #1731
@pyricau pyricau added this to the 2.2 milestone Jan 30, 2020
@ryanholden8
Copy link

Does this work when view models are loaded inside Compose? Can't seem to find anything on this in the docs. We can manually create a leak at the Activity (Single Activity since it's a full Compose App) and the leak is detected in our tests. However, when creating a leak in a view model the tests do not detect it.

@ryanholden8
Copy link

Opened this issue: #2678

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants