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

[ios/catalyst] fix memory leaks in *Cell #22067

Merged
merged 8 commits into from May 17, 2024

Conversation

jonathanpeppers
Copy link
Member

@jonathanpeppers jonathanpeppers commented Apr 25, 2024

Fixes: #20195
Context: https://github.com/AdamEssenmacher/MemoryToolkit.Maui/tree/main/samples

Retesting this sample with the ListView leak fixed, I noticed the sample reports 0 leaks! Unfortunately, it still displayed memory growing over time...

Taking a .gcdump snapshot, I noticed a cycle:

  • ViewCellRenderer.ViewTableCell -> ViewCell _viewCell -> ViewCellRenderer -> ViewTableCell

I was able to write a test that reproduces the leak, and I extended it for every type of *Cell like TextCell, ImageCell, SwitchCell, etc.

The fixes are:

  • ViewTableCell now uses a WeakReference<ViewCell> _viewCell

  • CellTableViewCell now uses a WeakReference<Cell> _cell

  • CellTableViewCell now uses a WeakEventManager for PropertyChanged, as the *Renderer subscribes to this event.

Note that I changed PropertyChanged to an event, which is an API change. (I can revisit this if needed)

@jonathanpeppers jonathanpeppers added platform/iOS 🍎 memory-leak 💦 Memory usage grows / objects live forever labels Apr 25, 2024
@jonathanpeppers jonathanpeppers marked this pull request as ready for review April 26, 2024 20:51
@jonathanpeppers jonathanpeppers requested a review from a team as a code owner April 26, 2024 20:51
@jsuarezruiz
Copy link
Contributor

Needs a rebase to get the updated snapshot for Issue18242Test and fix the build.

jsuarezruiz
jsuarezruiz previously approved these changes May 13, 2024
@jonathanpeppers
Copy link
Member Author

@jsuarezruiz do you know what's wrong with the one test lane? I wonder if it is caused by the changes here.

@PureWeen
Copy link
Member

/rebase

@PureWeen PureWeen force-pushed the CellLeaks branch 2 times, most recently from a9ff7f9 to 0e57c49 Compare May 16, 2024 15:08
jonathanpeppers and others added 4 commits May 16, 2024 10:53
Context: https://github.com/AdamEssenmacher/MemoryToolkit.Maui/tree/main/samples

Retesting this sample with the `ListView` leak fixed, I noticed the
sample reports 0 leaks! Unfortunately, it still displayed memory growing
over time...

Taking a `.gcdump` snapshot, I noticed a cycle:

* `ViewCellRenderer.ViewTableCell` -> `ViewCell _viewCell` -> `ViewCellRenderer` -> `ViewTableCell`

I was able to write a test that reproduces the leak, and I extended it
for every type of `*Cell` like `TextCell`, `ImageCell`, `SwitchCell`, etc.

The fixes are:

* `ViewTableCell` now uses a `WeakReference<ViewCell> _viewCell`

* `CellTableViewCell` now uses a `WeakReference<Cell> _cell`

* `CellTableViewCell` now uses a `WeakEventManager` for `PropertyChanged`,
  as the `*Renderer` subscribes to this event.

Note that I changed `PropertyChanged` to an event, which is an API
change. (I can revisit this if needed)
One test was crashing (even on rerun), so maybe this will fix
Copy link
Member

@PureWeen PureWeen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to try to rework this without needing the InternalPropertyChanged

@@ -135,44 +133,9 @@ protected void UpdateBackground(UITableViewCell tableViewCell, Cell cell)
SetBackgroundColor(tableViewCell, cell, uiBgColor);
}

[Obsolete("The ForceUpdateSizeRequested event is now managed by the command mapper, so it's not necessary to invoke this event manually.")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method was just a really complicated way of wiring up to _onForceUpdateSizeRequested and making sure to unwire from it.

We can just achieve this same thing through the commandmapper now

else
{
tvc.PropertyChanged -= HandlePropertyChanged;
CellPropertyChanged -= HandlePropertyChanged;
Copy link
Member

@PureWeen PureWeen May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this event now is just local to this class this isn't going to cause a circular reference. I opted to keep this event path because this maintains the timing of when HandlePropertyChanged will start and stop firing

@PureWeen PureWeen requested a review from jsuarezruiz May 16, 2024 20:53
@PureWeen PureWeen dismissed their stale review May 16, 2024 20:53

I reworked most of this PR so my review isn't relevant anymore

@PureWeen PureWeen merged commit e7f8467 into dotnet:main May 17, 2024
49 checks passed
@jonathanpeppers jonathanpeppers deleted the CellLeaks branch May 17, 2024 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-listview ListView and TableView memory-leak 💦 Memory usage grows / objects live forever platform/iOS 🍎
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ViewCells are not GC'ed on iOS, causing cascading memory leak
4 participants