Xamarin.Forms not freeing (and so leaking) UI elements when a Page is popped

依然范特西╮ 提交于 2019-12-23 05:38:11

问题


We're experiencing memory leaks in our Xamarin.Forms Android application, and I've been investigating using Profiler. This has led me to running Profiler against a very basic Forms app.

The very basic Forms app is based on the simple List->Detail template project which is available in Visual Studio For Mac.

All it does is navigate from a list page to a details page using the inbuilt navigation stack.

    async void OnItemSelected(object sender, SelectedItemChangedEventArgs args)
    {
        var item = args.SelectedItem as Item;
        if (item == null)
            return;

        await Navigation.PushAsync(new ItemDetailPage(new ItemDetailViewModel(item)));

        // Manually deselect item
        ItemsListView.SelectedItem = null;
    }

Going from List->Details->List 11 times and then taking a snapshot in Profiler reveals that we've got 372 BindablePropertyContext objects and 22 Labels (among other objects) created and not released:

I have tried setting Content to null and BindingContext to null in the OnDisappearing event of the Details page, and also putting an explicit GC.Collect() in the OnAppearing of the List page. Neither of these fix the problem.

This is not acceptable for an app which is intended to run continuously on a device!

So is there a core memory leak in Forms, or am I missing something?

I'm using Xamarin.Forms 2.5.0.121934


回答1:


Running this on a less powerful device (i.e. with less memory) showed that these objects were in fact GC'd, and so there is in fact no problem, and no leak.



来源:https://stackoverflow.com/questions/47796408/xamarin-forms-not-freeing-and-so-leaking-ui-elements-when-a-page-is-popped

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!