1

也许我提供的信息太少,这是我的错。这是我的项目:

登录窗口.xaml:

<Window x:Class="IEXM.LoginWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="LoginWindow" 
        Height="300" Width="300">
    <Grid Height="228">
        <Grid.ColumnDefinitions>
        <ColumnDefinition Width="214*" />
        <ColumnDefinition Width="64*" />
        </Grid.ColumnDefinitions>
        <Button Click="New_One" FontSize="50" Margin="0,0,0,113" Grid.ColumnSpan="2">New One</Button>
        <Button Click="Delete" FontSize="50" Margin="0,121,0,0" Grid.ColumnSpan="2">Delete</Button>
    </Grid>
</Window>

登录窗口.xaml.cs:

using System;
using System.Linq;
using System.Windows;

namespace IEXM
{
    public partial class LoginWindow : Window
    {
        MainWindow w;
        public LoginWindow()
        {
            InitializeComponent();
        }

        private void Delete(object sender, RoutedEventArgs e)
        {
            if (w == null)
                return;
            w.Close();
            w = null; 
            GC.Collect();
        }

        private void New_One(object sender, RoutedEventArgs e)
        {
            w = new MainWindow();
            w.Show();
        }
    }
}

MainWindow.xaml:

<Window x:Class="IEXM.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Width="380" Height ="265"
        xmlns:gifLib="clr-namespace:WpfAnimatedGif;assembly=WpfAnimatedGif" >
    <ScrollViewer Width="300" Height ="165" Name="sc">
        <ItemsControl  Name="wp_face">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel ItemHeight="40" ItemWidth="40"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <Image Name="img"  
                               Tag="{Binding source}" 
                               Style="{StaticResource CWFacePopupGifImageStyle}"/>
                    </WrapPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
</Window>

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace IEXM
(
    public partial class MainWindow : Window
    (
        public class ImageItem : INotifyPropertyChanged
        (
            protected string _source;

            public string source
            (
            get     ( return _source; }
            set
                (
                    _source = value;
                    OnPropertyChanged("source");
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged(string propertyName)
            (
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }

            public ImageItem(string _source)
            (
                this._source = _source;
            }
        }

        public MainWindow()
        (
            InitializeComponent();

            ObservableCollection<ImageItem> gifList = new ObservableCollection<ImageItem>();
            for (int i = 0; i < 107; ++i)
            (
                gifList.Add(new ImageItem(
                    "pack://application:,,,/Expression/f" + i.ToString().PadLeft(3, '0') + ".gif"));
            }
            wp_face.ItemsSource = gifList;
        }

        protected override void OnClosed(EventArgs e)
        (
            wp_face.ItemsSource = null;
            base.OnClosed(e);
        }
    }
}

和 app.xaml:

<Application x:Class="IEXM.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="LoginWindow.xaml"
             xmlns:gifLib="clr-namespace:WpfAnimatedGif;assembly=WpfAnimatedGif">
    <Application.Resources>
        <Style x:Key="CWFacePopupGifImageStyle" TargetType="Image">
            <Setter Property="Width" Value="36" />
            <Setter Property="Height" Value="36" />
            <Setter Property="gifLib:ImageBehavior.AnimatedSource" 
                    Value="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" />
            <Setter Property="gifLib:ImageBehavior.RepeatBehavior" Value="0" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="gifLib:ImageBehavior.RepeatBehavior" Value="Forever" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Application.Resources>
</Application>

在我点击“New One”之前占用了11992KB的内存,点击“New One”之后是50724KB,但是点击“Delete”之后就增加到了51996KB,之后就没有减少了。那么我的代码有什么问题?

PS:107个gif总共1.9MB。

4

1 回答 1

2

我替换MainWindowWindow运行了代码。据我所知,它没有产生任何内存泄漏,因此泄漏在您的代码中。如果没有更多信息,我们不能说它是什么,但是您可以尝试使用 CLR Profiler 来查找分配了哪些对象。

CLR Profiler 可以在这里找到:http ://clrprofiler.codeplex.com/

可以在这里找到一个很好的教程:http: //msdn.microsoft.com/en-us/library/ff650691.aspx 和这里: http: //www.codeproject.com/Articles/45292/NET-Best-Practice -No-1-Detecting-High-Memory-cons

于 2013-11-07T09:56:39.287 回答