Xamarin.Forms not freeing (and so leaking) UI elem

2019-06-09 11:16发布

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条回答
冷血范
2楼-- · 2019-06-09 11:33

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.

查看更多
登录 后发表回答