【发布时间】:2011-11-30 08:42:16
【问题描述】:
我创建了一个简单的 wpf C#,它带有一个文本框和 DispatcherTimer,它每秒显示调用 GC.GetTotalMemory(true) 的结果。每次调用返回的值都会稳步增加,并且任务管理器显示私有工作内存集也会增加。 这真的是内存泄漏,还是只是外观?在我的真实应用程序中,在每个滴答声中执行更多操作,内存泄漏似乎明显更高。 我的代码如下
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();
}
}
}
【问题讨论】: