Reputation: 73
Maybe I gave too little information, it's my fault. Here is my project:
LoginWindow.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>
LoginWindow.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);
}
}
}
and the 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>
Before I click "New One", it took 11,992KB memory, and after click "New One", it was 50,724KB, but after click "Delete", it raised to 51,996KB, and it didn't decrease after that. So what's wrong with my code?
PS:The 107 gifs are totally 1.9MB.
Upvotes: 1
Views: 321
Reputation: 2593
I replaced MainWindow
with Window
and ran the code. It did not produce any memory leaks as far as I could see so the leak is in your code. What it is we cannot say without more information, but you could try to use the CLR Profiler to find what objects are allocated.
The CLR Profiler can be found here: http://clrprofiler.codeplex.com/
And a nice tutorial can be found here: http://msdn.microsoft.com/en-us/library/ff650691.aspx and here: http://www.codeproject.com/Articles/45292/NET-Best-Practice-No-1-Detecting-High-Memory-cons
Upvotes: 2