CollectionView导致内存泄露?
2013-02-04 07:33
246 查看
本文将创建一个示例项目,运行后探查内存,发现本应被垃圾回收的UI控件没有被回收。进一步发现是CollectionView导致控件不能被回收。最后,通过查看.NET Framework源代码,发现其实不是内存泄露,虚惊一场。
GroupControl.xaml
GroupControl.xaml.cs
然后主窗口使用这个GroupControl,在窗口加载的时候往GroupControl里填数据,当用户选择GroupControl里任意一项的时候,卸载这个GroupControl。
MainWindow.xaml
MainWindow.xaml.cs
当卸载了GroupControl之后,尽管也调用了GC,我用.NET Memory Profiler查看,发现它还是存在。
图1
图2
图2表示GroupBox._contextStorage保存了我的GroupControl;ListBox._parent保存了前面的GroupBox; ItemsPresenter保存了前面的ListBox;以此类推。因为有对GroupControl的引用链存在,所以它无法被垃圾回收。
把MainWindow.cs的
但这样的解决方案留给人几点疑惑:
控件之间的相互引用不应阻碍垃圾回收,只要它们没有被外部的长生命周期的实例引用。
这个解决方案似乎不能得出什么一般性的原则来避免疑似由ContentPresenter引起的内存泄露。众所周知,WPF里大量使用ContentPresenter,难道都会泄露?
仔细查看内存探查的结果(图3),会发现ListCollectionView(也存在于图2中的第7行)并没有被垃圾回收。
图3
所以,需要一个更彻底的解决方案。
我在VS里输入ListCollectionView,然后按F12转到定义。我装了Resharper,做过查看.net源代码的配置,所以就可以转到ListCollectionView的源代码。可是不知为什么,ListCollectionView的代码是空的。于是我转到CollectionView。
然后查找哪里使用了CollectionView。Reshaper 7.0疑似跟以前相比改进过了,可以查找.net类库里使用的类,于是我找到了ViewTable.Purge(),而且ViewTable也正好在引用链里面。
图4
查找ViewTable.Purge()的调用方,可以找到ViewManager.Purge()。继续查找,定位到 DataBindEngine.GetViewRecord(object collection, CollectionViewSource key, Type collectionViewType, bool createView, Func<object, object> GetSourceItem):ViewRecord。所以差不多明白了,每当创建新的CollectionView的时候,就会调用Purge,就会删除未使用的旧的CollectionView。
于是尝试解决办法,在MainWindow.cs的GC.Collect()之后,加上
再探查内存,发现只有一个ListCollectionView,值为1234;而旧的“时间”“日期”的CollectionView已经被销毁了。
经过此番研究,结论是移除对ItemsControl的引用后,ItemsControl的CollectionView不会销毁,ItemsControl本身可能也不会销毁。如果再创建一个新的ItemsControl,并填充数据,旧的CollectionView和ItemsControl就会被销毁了。
发现问题
创建一个用户控件GroupControl,有AddGroup(object header, object[] items)方法。这个方法就是创建一个GroupBox,设置Header和GroupBox里面的ListBox.ItemsSource。GroupControl.xaml
<ContentControl x:Class="Gqqnbig.TranscendentKill.GroupControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <ItemsControl Name="selectionGroupPanel" x:FieldModifier="private" HorizontalAlignment="Left" VerticalAlignment="Top"/> </ContentControl>
GroupControl.xaml.cs
public partial class GroupControl { public GroupControl() { InitializeComponent(); } public event SelectionChangedEventHandler SelectionChanged; public void AddGroup(object header, object[] items) { GroupBox groupBox = new GroupBox(); groupBox.Header = header; ListBox listBox = new ListBox(); listBox.ItemsSource = items; listBox.SelectionChanged += listBox_SelectionChanged; groupBox.Content = listBox; selectionGroupPanel.Items.Add(groupBox); } void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (SelectionChanged != null) SelectionChanged(this, e); } }
然后主窗口使用这个GroupControl,在窗口加载的时候往GroupControl里填数据,当用户选择GroupControl里任意一项的时候,卸载这个GroupControl。
MainWindow.xaml
<Window x:Class="Gqqnbig.TranscendentKill.UI.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" x:ClassModifier="internal" Loaded="Window_Loaded_1"> </Window>
MainWindow.xaml.cs
internal partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded_1(object sender, RoutedEventArgs e) { Tuple<string, object[]>[] cps = new Tuple<string, object[]>[2]; cps[0] = new Tuple<string, object[]>("时间", new[] { (object)DateTime.Now.ToShortTimeString() }); cps[1] = new Tuple<string, object[]>("日期", new[] { (object)DateTime.Now.ToShortDateString() }); GroupControl win = new GroupControl(); for (int i = 0; i < cps.Length; i++) { ContentPresenter[] items = new ContentPresenter[cps[i].Item2.Length]; for (int j = 0; j < cps[i].Item2.Length; j++) items[j] = new ContentPresenter { Content = cps[i].Item2[j] }; win.AddGroup(cps[i].Item1, items); } win.SelectionChanged += win_SelectionChanged; Content = win; } void win_SelectionChanged(object sender, SelectionChangedEventArgs e) { GroupControl win = (GroupControl)this.Content; win.SelectionChanged -= win_SelectionChanged; Content = null; GC.Collect(); } }
当卸载了GroupControl之后,尽管也调用了GC,我用.NET Memory Profiler查看,发现它还是存在。
图1
图2
图2表示GroupBox._contextStorage保存了我的GroupControl;ListBox._parent保存了前面的GroupBox; ItemsPresenter保存了前面的ListBox;以此类推。因为有对GroupControl的引用链存在,所以它无法被垃圾回收。
不彻底的解决方案
从引用链可以发现,ContentPresenter引用了父元素ListBoxItem,所以尝试从ContentPresenter入手。不生成ContentPresenter,直接用原始的集合。把MainWindow.cs的
for (int i = 0; i < cps.Length; i++) { ContentPresenter[] items = new ContentPresenter[cps[i].Item2.Length]; for (int j = 0; j < cps[i].Item2.Length; j++) items[j] = new ContentPresenter { Content = cps[i].Item2[j] }; win.AddGroup(cps[i].Item1, items); }改为
for (int i = 0; i < cps.Length; i++) { //ContentPresenter[] items = new ContentPresenter[cps[i].Item2.Length]; //for (int j = 0; j < cps[i].Item2.Length; j++) // items[j] = new ContentPresenter { Content = cps[i].Item2[j] }; win.AddGroup(cps[i].Item1, cps[i].Item2); }。这样探查内存,发现GroupControl消失了。问题疑似解决。
但这样的解决方案留给人几点疑惑:
控件之间的相互引用不应阻碍垃圾回收,只要它们没有被外部的长生命周期的实例引用。
这个解决方案似乎不能得出什么一般性的原则来避免疑似由ContentPresenter引起的内存泄露。众所周知,WPF里大量使用ContentPresenter,难道都会泄露?
仔细查看内存探查的结果(图3),会发现ListCollectionView(也存在于图2中的第7行)并没有被垃圾回收。
图3
所以,需要一个更彻底的解决方案。
寻找原因/彻底的解决方案
图3说明ListCollectionView跟外部做了什么交互,导致自己被引用上了,所以一连串都不能被回收。我在VS里输入ListCollectionView,然后按F12转到定义。我装了Resharper,做过查看.net源代码的配置,所以就可以转到ListCollectionView的源代码。可是不知为什么,ListCollectionView的代码是空的。于是我转到CollectionView。
然后查找哪里使用了CollectionView。Reshaper 7.0疑似跟以前相比改进过了,可以查找.net类库里使用的类,于是我找到了ViewTable.Purge(),而且ViewTable也正好在引用链里面。
图4
查找ViewTable.Purge()的调用方,可以找到ViewManager.Purge()。继续查找,定位到 DataBindEngine.GetViewRecord(object collection, CollectionViewSource key, Type collectionViewType, bool createView, Func<object, object> GetSourceItem):ViewRecord。所以差不多明白了,每当创建新的CollectionView的时候,就会调用Purge,就会删除未使用的旧的CollectionView。
于是尝试解决办法,在MainWindow.cs的GC.Collect()之后,加上
ListBox listBox = new ListBox(); int[] n = { 1, 2, 3, 4 }; listBox.ItemsSource = n; Content = listBox;
再探查内存,发现只有一个ListCollectionView,值为1234;而旧的“时间”“日期”的CollectionView已经被销毁了。
经过此番研究,结论是移除对ItemsControl的引用后,ItemsControl的CollectionView不会销毁,ItemsControl本身可能也不会销毁。如果再创建一个新的ItemsControl,并填充数据,旧的CollectionView和ItemsControl就会被销毁了。
相关文章推荐
- 解决handler可能导致的内存泄露
- IOS的ARC导致内存泄露的几种情况和具体解决方案
- 加载webView 内存泄露 导致内存暴涨的几种解决方案
- VC:快速侦测断言错误导致的内存泄露
- 使Handler内部类不导致内存泄露的解决方法
- 全局NSTimer导致的内存泄露及解决办法
- pthread_exit导致的内存泄露问题。
- performSelector延时调用导致的内存泄露
- PHP脚本内存泄露导致Apache频繁宕机解决方法
- 【转】performSelector延时调用导致的内存泄露
- surfaceview导致gallery2内存泄露
- 使用self. 导致的内存泄露
- inet_ntoa导致内存泄露
- Flex 中可能导致内存泄露的地方
- Android开发编码规范导致的内存泄露问题
- 事件导致的内存泄露及Weak Event Pattern
- 静态变量导致的内存泄露
- performSelector延时调用导致的内存泄露
- Android:Handler,内部类导致的可能内存泄露
- Handler导致内存泄露分析