TabControl.Items.Remove(TabItem) does not free memory used by TabItem

微笑、不失礼 提交于 2019-12-22 12:41:45

问题


I am having a WPF application where when I close a TabItem from TabControl using TabControl.Items.Remove(TabItem) does not free memory used by TabItem. It just makes TabItem invisible and TabItem object still remains in the memory. Is there any way to remove this TabItem object from memory ? Any help would be appreciated.


回答1:


As far as I know all eventhandlers must be 'detached', also you should release all bindings on your tabItem.

Also If I am not mistaken CommandBindings, KeyGestures can also cause the leak.

Try using CLR Profiler. It has saved me a lot of hours many times, though I haven't used it with WPF. But I'm pretty sure it can handle it.




回答2:


You need to cut out an object from its parent completely for it to be garbage collected. That means also the events (-= blah).

Even that done, the object might still be in memory until the next sweep of the GC.

You can force a call of the GC but it's not recommended. http://msdn.microsoft.com/en-us/library/system.gc.aspx



来源:https://stackoverflow.com/questions/3167943/tabcontrol-items-removetabitem-does-not-free-memory-used-by-tabitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!