Search code examples
xamarin.formsxamarin.iosxamarin.androidxamarin.uwp

Does Xamarin Forms have memory leak on UWP?


I wrote UWP but this can be also on Android on IOS because I profiled only UWP application using VS2017.

Steps to create problem. - Open VS 2017 and start a new xamarin forms project by selecting tabbed page or masterdetail page. No need to write any single code.

Problem;

  • First snapshot is after application is loaded.

  • 2nd one has taken after selecting an item in the list and navigating to the ItemDetailsPage

  • 3rd snapshot was taken after navigating back to ItemsPage

enter image description here

Expections; to not see ItemDetailsPage on 3rd snapshot because I am navigating back and this page is popped from the navigation stack. so it should be removed, collected by GC or disposed.

Here is the 3rd snapshot details;

enter image description here

Do I read this snapshot wrong or there is something wrong with the xamarin forms applications?

EDIT: Below screenshot also stats that there is "cycle detected". what does that mean? i thought cycles cause memory leaks, dont they?

enter image description here


Solution

  • not see ItemDetailsPage on 3rd snapshot because I am navigating back and this page is popped from the navigation stack. so it should be removed, collected by GC or disposed.

    If you try navigating to different detailed pages then come back and take a snapshot again, you will find the count of ItemDetailPage stays 1.

    Sometimes, for performance's sake, objects like page or resources will be properly cached and reused. If you have a page which contains large images or resources, you should try to dispose them manually to reduce memory usage.

    Update:

    Now question is how to properly dispose it? where to do that? I keep reading OnDissapearing method which is not safe for me as it can be called on navigating forward , not only navigating back in the stack. what is your suggestion for that?

    If you don't want to dispose your resources for navigating forward, you can define a status parameter when navigating forward, do not dispose the resource. ex:

    bool forwardNavigating=false;
    ...
     protected override void OnDisappearing()
     {
        base.OnDisappearing();
        if (!forwardNavigating)
        {
            //dispose your resources
        }
     }
    

    elow screenshot also stats that there is "cycle detected". what does that mean? i thought cycles cause memory leaks, dont they?

    cycle detected means:

    An object can reference a second object that directly or indirectly holds a reference to the first object.

    Please refer to Circular references of Analyze .NET Framework memory issues.

    Personal thought:

    I don't think it is a memory issue in terms of cycle detected. Under some situations we may need to visit parent object when we are in it's children. Thus we need a property Parent while the parent is still referencing the children. This happens a lot in views. I don't know how the analyzer works and whether it is the situation I described above. What I want to say is cycle detected doesn't equals Memory Leaks.