WPF常见内存泄露

Event handlers leak

This type of leak occurs when subscribing an object (let's call it listener) to an event of some other object (let's call it source). For example: Timer1.Tick += OnTimer; During subscription, the source object gets a reference to the event handler of the listener object. If you delete the listener, this reference will prevent it from being garbage collected. dotMemory automatically finds objects that are referenced in event handlers but are never unsubscribed from corresponding events.

 

Wpf binding leak

Breaking WPF data binding patterns also can cause a memory leak. After you perform data binding to some property of a source object, the binding target object starts to listen for property change notifications. If the property is not aDependencyProperty object and the target object does not implement the INotifyPropertyChanged interface, a memory leak in source object and in every object to which source object refers may occur. dotMemory detects such binding pattern violations and shows you the list of objects that may potentially cause this leak type.

The leak will not take place in case the OneTime binding mode is used to update the target.

 

 

Wpf collection binding leak

This leak is similar to the WPF binding leak described above. If there is binding to a collection that does not implement the INotifyCollectionChanged interface, WPF creates a strong reference to this collection. As a result, it stays in memory for the entire application lifetime. dotMemory detects and shows you such objects.

 

Dependency property leak

This leak occurs due to quite the same reasons as the event handlers leak. GC will not collect objects subscribed onDependencyProperty changes through the AddValueChanged method until they are unsubscribed using the RemoveValueChanged method. dotMemory detects and shows you all such A objects.

 

x:Name WPF leak

This leak takes place because of the following WPF peculiarity: WPF creates a strong global reference to the UI element that is declared in XAML and uses the x:Name directive. For example:< XNameTest:UserControl1 Grid.Row="0" x:Name="myControl1"/> Thus, if you dynamically remove the element declared in such a way, it will still be in memory.

 

 

转载于:https://www.cnblogs.com/nocanstillbb/p/10300302.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值