2010-08-04 36 views
5

任何人都可以解釋爲什麼這個小應用程序的內存使用量不斷增加嗎?帶計時器的簡單應用程序正在消耗內存嗎?

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; 
    } 
} 

謝謝! Pika81

+0

我們在這裏說話有多快? – ChaosPandion 2010-08-04 14:55:36

+0

此外,內存使用是否穩定/上限在一定水平? – theburningmonk 2010-08-04 15:04:08

+0

@Greg - 你知道我們也不知道'Form1'中發生了什麼。 – ChaosPandion 2010-08-04 15:13:36

回答

1

我的建議是打開Windbg並找出內存中存在什麼對象。
同意我們的開發者通常把它作爲最後的選擇,但在這種情況下,它會給你的記憶的確切原因增加

+1

那還是個不錯的分析器。 – ChaosPandion 2010-08-04 15:20:46

+0

我運行一個分析器並在5分30秒和14分14秒後運行GC。字符串實例似乎得到清理。任務管理器不可靠。 – 2010-08-04 16:16:37

1

我通過DefaultTraceListener源挖掘,我發現這一點:

private void WriteLine(string message, bool useLogFile) 
{ 
    if (base.NeedIndent) 
    { 
     this.WriteIndent(); 
    } 
    this.Write(message + "\r\n", useLogFile); 
    base.NeedIndent = true; 
} 

所以內存使用量可能增長過於緩慢的GC立即作出反應。

+0

+1的內存量。 – 2010-08-04 15:09:11

1

如果您只查看任務管理器以查看您的進程正在使用多少內存,則可能得不到準確的讀數。

對這篇文章的讀: http://www.itwriting.com/dotnetmem.php

它解釋了一些使用任務管理器來衡量你的.NET應用程序的內存使用情況的手段不足的。

3

我們假定你是內存使用量不應增加。錯誤的假設,這只是而不是 .NET垃圾收集器或Windows堆管理器如何工作。它們都可以通過使用可用的內存而不是不斷釋放和重新分配內存來有效地工作。

讓它運行一個星期。如果您將Interval縮小,可能會更快。同時最小化壯觀效果的形式。