Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple app with timer is eating memory away?

Can anybody explain why this tiny app's memory usage keeps increasing ?

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

Thanks! Pika81

like image 946
Janiek Buysrogge Avatar asked Aug 04 '10 14:08

Janiek Buysrogge


2 Answers

You are assuming that the memory usage should not increase. Wrong assumption, that's just not how either the .NET garbage collector or the Windows heap manager work. Both of them work efficiently by using memory that's available for use instead of constantly releasing and reallocating memory.

Let it run for a week. Might go quicker if you make the Interval smaller. Also minimize the form for spectacular effects.

like image 180
Hans Passant Avatar answered Sep 18 '22 03:09

Hans Passant


My suggestion would be to crank up Windbg and find out what objects exist in memory.
Agreed that we devs generally use it as the last option but in this case, it would give you the exact reason for the memory increase

like image 28
Bharath K Avatar answered Sep 17 '22 03:09

Bharath K