UWP compiled binding x:Bind produces memory leaks

前端 未结 2 1689
后悔当初
后悔当初 2021-02-02 11:25

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:

<         


        
相关标签:
2条回答
  • 2021-02-02 11:46

    Yes it does cause memory leak, to prevent that you can use following steps:

    1. Use IoC like UnityContainer and make your ViewModel or View ContainerControlLifeTime
    2. Assign null to ViewModel property at xaml.cs once you move out of UI.
    0 讨论(0)
  • 2021-02-02 11:53

    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-using-compiled-x-bind-bindings

    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.

    0 讨论(0)
提交回复
热议问题