Skip to content

[controls] fix memory leak in CollectionView#14329

Merged
jonathanpeppers merged 2 commits intodotnet:mainfrom
jonathanpeppers:windows-collectionview-leak
Apr 13, 2023
Merged

[controls] fix memory leak in CollectionView#14329
jonathanpeppers merged 2 commits intodotnet:mainfrom
jonathanpeppers:windows-collectionview-leak

Conversation

@jonathanpeppers
Copy link
Copy Markdown
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
@jonathanpeppers jonathanpeppers force-pushed the windows-collectionview-leak branch from f871ff3 to 6f74a43 Compare April 10, 2023 19:37
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 force-pushed the windows-collectionview-leak branch from 6f74a43 to fe9c697 Compare April 11, 2023 03:40
@jonathanpeppers jonathanpeppers marked this pull request as ready for review April 11, 2023 13:36
@rmarinho rmarinho requested review from hartez and rmarinho April 11, 2023 14:51
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! perf/memory-leak 💦 Memory usage grows / objects live forever (sub: perf)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak while repeatedly navigating between pages

4 participants