Xamarin iOS memory leaks everywhere

前端 未结 5 1571
夕颜
夕颜 2020-11-30 19:06

We\'ve been using Xamarin iOS for the last 8 months and developed a non-trivial enterprise app with many screens, features, nested controls. We\'ve done our own MVVM arch, c

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 19:37

    Couldn't be agree more with the OP that "Garbage Collection is essentially broken in Xamarin".

    Here's an example shows why you have to always use a DisposeEx() method as suggested.

    The following code leaks memory:

    1. Create a class the inherits UITableViewController

      public class Test3Controller : UITableViewController
      {
          public Test3Controller () : base (UITableViewStyle.Grouped)
          {
          }
      }
      
    2. Call the following code from somewhere

      var controller = new Test3Controller ();
      
      controller.Dispose ();
      
      controller = null;
      
      GC.Collect (GC.MaxGeneration, GCCollectionMode.Forced);
      
    3. Using Instruments you will see that there are ~ 274 persistent objects with 252 KB never collected.

    4. Only way to fix this is add DisposeEx or similar functionality to the Dispose() function and call Dispose manually to ensure disposing == true.

    Summary: Creating a UITableViewController derived class and then disposing/nulling will always cause the heap to grow.

提交回复
热议问题