I have created a simple wpf C# with a single text box and DispatcherTimer that displays the result of a call to GC.GetTotalMemory(true) every second. The value returned increases steadily with each call, and task manager shows that the private working memory set increases also.
Is this really a memory leak, or just the appearance of one? In my real app that does a lot more within each tick the memory leakages appear significantly higher.
My code is as follows
xaml
<Window x:Class="TestWPFApplication.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBox Name="memoryuseage"></TextBox>
</Grid>
</Window>
xaml.cs
namespace TestWPFApplication
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
System.Windows.Threading.DispatcherTimer tmr;
public Window1()
{
InitializeComponent();
tmr = new System.Windows.Threading.DispatcherTimer();
tmr.Interval = new System.TimeSpan(0, 0, 1);
tmr.Tick += new EventHandler(StaticTick);
tmr.Start();
}
void StaticTick(object o, EventArgs sender)
{
memoryuseage.Text = GC.GetTotalMemory(true).ToString();
}
}
}
Just the appearance. A steady increase is normal, it isn’t a leak until you can crash it by running it long enough. But that could take days with a small leak.
Your memory usage ought to level off, but only after a considerable time. You might be able to speed that up by using GC.Collect() here (every 10th tick or so).
For a serious diagnosis you will need a memory-profiler.
For example: http://en.wikipedia.org/wiki/CLR_Profiler