问题
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