Xamarin.Forms不能释放(因此泄漏)UI元素在弹出页面时 [英] Xamarin.Forms not freeing (and so leaking) UI elements when a Page is popped

查看:394
本文介绍了Xamarin.Forms不能释放(因此泄漏)UI元素在弹出页面时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在Xamarin.Forms Android应用程序中遇到内存泄漏,并且我一直在使用Profiler进行调查.这使我不得不针对一个非常基本的Forms应用程序运行Profiler.

非常基本的Forms应用程序基于简单的List-> Detail模板项目,该项目可在Visual Studio for Mac中使用.

它所做的就是使用内置的导航堆栈从列表页面导航到详细信息页面.

    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;
    }

从列表"->详细信息"->列表"中浏览11次,然后在Profiler中进行快照显示,我们已经创建了372个BindablePropertyContext对象和22个标签(除其他对象之外):

我曾尝试在详细信息"页面的 OnDisappearing 事件中将 Content 设置为null,将 BindingContext 设置为null,并尝试放置一个显式GC列表页面的OnAppearing中的.Collect().这些都不能解决问题.

对于要在设备上连续运行的应用程序来说,这是不可接受的!

那么Forms中是否存在核心内存泄漏,或者我缺少什么?

我正在使用Xamarin.Forms 2.5.0.121934

解决方案

在功能更弱的设备(即内存更少)上运行此程序,表明这些对象实际上是GC处理的,因此实际上没有问题,而且没有泄漏.

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

解决方案

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.

这篇关于Xamarin.Forms不能释放(因此泄漏)UI元素在弹出页面时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆