当前位置:编程学习 > wap >>

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 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,