[英]Simple app with timer is eating memory away?
谁能解释为什么这个小应用程序的内存使用量持续增加?
static class Program
{
private static System.Timers.Timer _TestTimer;
[STAThread]
static void Main()
{
_TestTimer = new System.Timers.Timer();
_TestTimer.Interval = 30;
_TestTimer.Elapsed += new System.Timers.ElapsedEventHandler(_TestTimer_Elapsed);
_TestTimer.Enabled = true;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
static void _TestTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
string test = "tick";
Trace.WriteLine(test);
test = null;
}
}
谢谢! 皮卡81
您假设内存使用量不应增加。 错误的假设是,.NET垃圾收集器或Windows堆管理器都不是这样工作的。 它们都通过使用可用的内存来有效地工作,而不是不断释放和重新分配内存。
让它运行一周。 如果将时间间隔减小,则可能会更快。 同时最小化形式以获得壮观的效果。
我正在挖掘DefaultTraceListener
的来源,发现了这一点:
private void WriteLine(string message, bool useLogFile)
{
if (base.NeedIndent)
{
this.WriteIndent();
}
this.Write(message + "\r\n", useLogFile);
base.NeedIndent = true;
}
因此,内存使用量可能增长得太慢,以至于GC无法立即做出反应。
如果仅查看任务管理器以查看您的进程正在使用多少内存,则可能无法获得非常准确的读数。
阅读本文: http : //www.itwriting.com/dotnetmem.php
它解释了使用TaskManager作为衡量.Net应用程序的内存使用情况的方法的一些不足。
我的建议是启动Windbg并找出内存中存在哪些对象。
同意我们的开发人员通常将其用作最后一个选项,但是在这种情况下,它将为您提供内存增加的确切原因
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.