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

[controls] fix memory leak in CollectionView #14329

Merged

Conversation

jonathanpeppers
Copy link
Member

@jonathanpeppers jonathanpeppers commented Mar 31, 2023

Fixes #10578
Context: https://github.com/Vroomer/MAUI-navigation-memory-leak.git

After testing the above sample, I found that adding a CollectionView to a Page, makes it and the entire page live forever.

Android & Windows:

  • MauiRecyclerView and StructuredItemsViewHandler respectively subscribed to ItemsLayout.PropertyChanged. This kept the CollectionView alive -> all the way up to the Page.

  • Switched to using WeakNotifyPropertyChangedProxy solved the issue for these two platforms.

iOS:

  • Generally had a small "nest" of circular references. Initially, I saw CollectionView, UICollectionView, and various helper classes that would live forever.
* ItemsViewController : UICollectionViewController -> 
  * ObservableItemsSource ->
    * UICollectionViewController and UICollectionView

* UICollectionView ->
  * ItemsViewLayout : UICollectionViewFlowLayout ->
    * Func<UICollectionViewCell> ->
      * ItemsViewController : UICollectionViewController ->
        * UICollectionView

I switched to using WeakReference<T> to break the circular references. This required several null checks, where null references were not possible before.

After these changes my tests pass, yay!

@Eilon Eilon added the area-controls-collectionview CollectionView, CarouselView, IndicatorView label Mar 31, 2023
Fixes: dotnet#10578
Context: https://github.com/Vroomer/MAUI-navigation-memory-leak.git

After testing the above sample, I found that adding a `CollectionView`
to a `Page`, makes it and the entire page live forever.

Android & Windows:

* `MauiRecyclerView` and `StructuredItemsViewHandler` respectively
  subscribed to `ItemsLayout.PropertyChanged`. This kept the
  `CollectionView` alive -> all the way up to the `Page`.

* Switched to using `WeakNotifyPropertyChangedProxy` solved the issue
  for these two platforms.

iOS:

* Generally had a small "nest" of circular references. Initially, I saw
  `CollectionView`, `UICollectionView`, and various helper classes that
  would live forever.

* `ItemsViewController : UICollectionViewController` ->
  * `ObservableItemsSource` ->
    * `UICollectionViewController` and `UICollectionView`

* `UICollectionView` ->
  * `ItemsViewLayout : UICollectionViewFlowLayout` ->
    * `Func<UICollectionViewCell>` ->
      * `ItemsViewController : UICollectionViewController` ->
        * `UICollectionView`

I switched to using `WeakReference<T>` to break the circular references.
This required several null checks, where `null` references were not
possible before.

After these changes my tests pass, yay!
@jonathanpeppers jonathanpeppers marked this pull request as ready for review April 11, 2023 13:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView fixed-in-8.0.0-preview.4.8333 Look for this fix in 8.0.0-preview.4.8333! memory-leak 💦 Memory usage grows / objects live forever
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory leak while repeatedly navigating between pages
4 participants