In latest XF nuget 2.4.0, Xamarin.iOS Garbage Collector no longer use reference counting method?

ChrisOnXamChrisOnXam CAUniversity ✭✭

Hey guys,
I have been developing Xamarin since XF 1.0. Memory leak was a huge problem from XF 1.0 till now XF 2.4. I have always been implementing Dispose Pattern in all my ContentPage and ViewModel classes to make sure Event handlers, Message Subscribers, object references are all removed and set to null either in OnDisappearing() or in Dispose() function of each class.

Here is something very intriguing. After I upgraded my project to Xamarin.Forms 2.4.0.280 from 2.3.3.180. I found out this type of code that used to create memory leak in Xamarin.iOS no longer occur.
Code Example:
public partial class ContactUsPage : ContentPage
{
public ContactUsPage ()
{
InitializeComponent ();
}

protected override void OnAppearing ()
{
    //Lets say I have button named "btn" declared in Xaml page
    btn.Clicked += OnButtonClicked

    MessagingCenter.Subscribe<CoreMessenger, string>(this, "AMessageType", (sender, arg) =>
    {
        //some code 
    });
}

protected override void OnDisappearing ()
{
    //btn.Clicked -= OnButtonClicked
    //MessagingCenter.Unsubscribe<CoreMessenger, string>(this, "AMessageType");
}

}

Prior to Xamarin.Forms 2.4, If we Push and Pop this page as a Modal multiple time, we will see multiple instances of this page persist in memory and will never be Garbage Collected due to Event Handler and Subscriber are not removed and they created object references pointing back to the ContactUsPage instances, unless we uncomment the code in OnDisappearing () function.

The above code no longer creates memory leak in Xamarin iOS when compiled in Xamarin.Forms 2.4.0.280, although it is clearly leaking memory according to my knowledge of how reference counting garbage collection works. Did Xamarin change the GC method for Xamarin.iOS? So it is doing mark-and-sweep like Xamarin.Android now?

Can someone tell me what changed and why?
Thanks a ton!

Sign In or Register to comment.