我正在使用 PerfView 来发现内存泄漏。
比较两个快照后,我注意到在 PerView 中的 RefTree -> static vars 选项卡下。
MyPageDependencyProperty
我的占 Inc% 的 78.9%。
MyPageDependencyProperty
不应该在那里,因为我关闭了它所属的 xaml 窗口。
我不使用 AddValueChanged
这可能会导致内存泄漏。
DependencyProperty
揭示 ObservableCollection<object>
.
有谁知道我可以解决这个问题吗?
谢谢
最佳答案
与其像这样定义您的属性,不如创建一个静态 ObservableCollection。
public static readonly DependencyProperty SomePropertyProperty = DependencyProperty.Register(typeof(..), typeof(..),....,.... , new ObservableCollection<...>());
你应该这样做:
public static readonly DependencyProperty SomePropertyProperty = DependencyProperty.Register(typeof(..), typeof(..),...., null);
public MyControl()
{
this.SomeProperty = new ObservableCollection<...>();
}
您的问题会神奇地消失。 :)
关于c# - DependencyProperty内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19974061/