CollectionView导致内存泄露?
本文将创建一个示例项目,运行后探查内存,发现本应被垃圾回收的UI控件没有被回收。进一步发现是CollectionView导致控件不能被回收。最后,通过查看.NET Framework源代码,发现其实不是内存泄露,虚惊一场。发现问题创建一个用户控件GroupControl,有AddGroup(object header, object[] items)方法。这个方法就是创建一个GroupBox,设置Header和GroupBox里面的ListBox.ItemsSource。GroupControl.xaml[html]<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[csharp]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[html]<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[csharp]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的[csharp]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);}改为[csharp]for (int i = 0; i < cps.Length; i++){//ContentPresenter[] items = new ContentPresenter[cps[i].Item2.Length];//for (int j = 0; j < cps补充:移动开发 , IOS ,
上一个:iOS中plist的创建,数据写入与读取
下一个:IO中的字节流练习
- 更多wap疑问解答:
- 新人求助QPainter
- 为什么程序都退出了还可以收到推送?如果大多设备都可以推送那运营商怎么办?
- qt 4.7 sqlserver2000 存储过程调用
- 关于ANDROID4.0.1编译问题!
- Android FrameBuffer读屏幕30秒后mmap失败
- 联通粗定位用java程序如何来请求和接受数据
- 为什么QT运行Android平台的程序时,mouseMoveEvent事件响应的间隔时间很长??????????
- android与PC蓝牙通讯
- 指定大小的label 内容可变,如果内容超出label的宽度,将未能显示的部分显示在另一个label上
- Android调试
- android如何通过wifi连接无线打印机
- 运行程序,release目录下产生一个乱码文件夹
- 分享个某机构最新安卓资料,自己验证了
- service启动不起来,掉不了service connection
- 求助:QT5.0 没有QPrinter吗