UWP编译结合X:绑定产生内存泄漏 [英] UWP compiled binding x:Bind produces memory leaks

查看:228
本文介绍了UWP编译结合X:绑定产生内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发应用程序UWP我最近发现了不少的内存泄漏防止我的网页由GC收集。
我有一个ContentPresenter我的网页像:

While developing UWP application I recently found quite a few memory leaks preventing my pages from being collected by GC. I have a ContentPresenter on my page like:

<ContentControl Grid.Column="0" Grid.Row="1" Content="{x:Bind ViewModel.Schedule, Mode=OneWay}">
</ContentControl>

在我删除内容或动态替换{}绑定 - 网页收集时,我从浏览。否则其保持在存储器中。
是它的bug或我做错了什么?有没有一种方式来释放和清除从航行中的所有绑定

After I remove the Content, or replace it with dynamic {Binding} -- page is collected when I navigate from it. Otherwise it remains in memory. Is it bug or I'm doing something wrong? Is there a way to release and clear ALL the bindings on navigating from?

更新:
这似乎是一个已知的问题微软内部为指出的此处
,而据我可以通过我自己的测试/应用程序的使用情况请参阅由x保存了数据:绑定一段时间后仍然被收集起来,当你,例如,浏览到同一页面或创建相同一段时间控制。我看得出来创建新的对象,却得到了回收旧的在一些点。

UPDATE: It seems to be a known problem inside Microsoft as was stated here. But as far as I can see through my own test/app usage the data that is preserved by the x:Bind still gets collected after some time, when you, for example, navigate to the same pages or create the same controls for some time. I could see that new objects were created, but old ones at some point got collected.

所以对我来说,似乎并没有被造成了内存的急剧问题,它只是不允许对象得到尽快动态绑定将收集。

So for me it doesn't seem to be a drastic problem causing out of memory, it only doesn't allow objects get collected as quickly as dynamic binding will.

推荐答案

我已经创建了微软的错误连接,因为这一问题。

I have created bug on Microsoft connect because of this issue.

https://connect.microsoft.com/VisualStudio/feedback/details/3077894/memory-leaks-in-c-uwp-apps-使用编译-X绑定绑定

变通解决此问题是卸载的页面事件处理程序显式调用Bindings.StopTracking()。这是因为编译绑定不使用弱事件模式,并直接订阅的INotifyPropertyChanged的PropertyChanged事件。它的内存泄漏的原因。退订事件,你可以调用Bindings.StopTracking()。编译绑定代码不会自动调用它。

Work around for this issue is to explicitly call Bindings.StopTracking() at page Unloaded event handler. It's because compiled bindings doesn't use "weak event" pattern and does subscribe to PropertyChanged event of INotifyPropertyChanged directly. It's a cause of memory leak. To unsubscribe events you can call Bindings.StopTracking(). Compiled bindings code doesn't call it automatically.

这篇关于UWP编译结合X:绑定产生内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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