Xamarin.forms: [Bug] Collection view has memory leak in iOS

Created on 1 Mar 2020  路  7Comments  路  Source: xamarin/Xamarin.Forms

Description

the Collection view leaks under iOS when binding to an ObservableCollection

Steps to Reproduce

  1. Clone repo https://github.com/hulluP/CollectionViewMemLeak
  2. run tipcalc.Forms.ios on the simulator
  3. click on show list
  4. navigate back via arrow on top left.
  5. lick on "check leak"
  6. the label below the "check leak" button shows if the application leaked

Expected Behavior

the above steps work fine when running this on android

Actual Behavior

on iOS the collectionview leaks, the way to isolate that is to disable the collection view. Comment out the following lines in the ScrollViewX class and the memleak check will not report any leaks:
line 35: stackLayout.Children.Add(DaCollectionView);
line 81: set.Bind(DaCollectionView).For(v => v.ItemsSource).To(vm => vm.ColMonkeys).OneWay();
line 82: set.Bind(DaCollectionView).For("ItemClick").To(vm => vm.MonkeySelectedCommand);

Basic Information

  • Version with issue: 4.5.0.35
  • Last known good version: -
  • IDE: Visual Studio Mac
  • Platform Target Frameworks:

    • iOS: 13.3

    • Android: API 28

    • UWP:

  • Android Support Library Version:
  • Nuget Packages: Xamarin Forms
  • Affected Devices: iOS

Screenshots

Reproduction Link

https://github.com/hulluP/CollectionViewMemLeak

Workaround

collectionview iOS 馃崕 bug

Most helpful comment

+1 to this issue. We're seeing the same thing in our iOS app.

In trying to find a workaround, I've discovered the following:

  • This only happens when applying an ItemTemplate to a CollectionView
  • ItemsViewController.UpdateTemplatedCell() calls cell.Bind() which results in TemplatedCell subscribing to an Element's MeasureInvalidated event. I can't see where this is being unsubscribed from.
  • ItemsViewController.UpdateTemplatedCell() also subscribes to the cell's ContentSizeChanged event. I can't see where this is being unsubscribed from.

Hope this helps.

cheers,
Mike

All 7 comments

@hulluP I tried to git clone your repo and build your app, but the iOS build is throwing errors.

HI @hartez

Sorry I did a local change to test which was not supposed to be pushed. I cleaned up the repo it does work again.

HP

+1 to this issue. We're seeing the same thing in our iOS app.

In trying to find a workaround, I've discovered the following:

  • This only happens when applying an ItemTemplate to a CollectionView
  • ItemsViewController.UpdateTemplatedCell() calls cell.Bind() which results in TemplatedCell subscribing to an Element's MeasureInvalidated event. I can't see where this is being unsubscribed from.
  • ItemsViewController.UpdateTemplatedCell() also subscribes to the cell's ContentSizeChanged event. I can't see where this is being unsubscribed from.

Hope this helps.

cheers,
Mike

I've got stacks of Problems with CollectionView on iOS, should I just use some other Collection Scrolling View at this point? Following multiple of these bugs that have been open 6 months +

@Kylar182 - due to the iOS memory issues, we've avoided CollectionView completely and stayed with ListView in our app. Probably not ideal if you need some of the CollectionView UI improvements though.

@michaelb-yambay This is my first Xamarin App and Everyone said use CollectionView as it's the new Microsoft Recommendation. I know some of the problems are native to iOS's CollectionView itself so you can't fully Blame Xamarin but you'd think they'd implement work arounds after a year.

Got the same problem here, can't use collectionview without big leaks of memory.

Was this page helpful?
0 / 5 - 0 ratings