I am seeing a constant increase in Private bytes of a process that
continuosly creates timers and disposes them. This increase is in the
unmanaged memory and not the .NET managed memory(as shown by PerfMon
counters). Has anyone seen this kind of leak before?
The leak is very small, creating and destroying 100 timers every 8 seconds
seems to leak about 26 MB over 6 days.
This is the code of a simple WinForms app that is simply creating timers and
destroying them.(timer1_Tick runs every 4 seconds)
namespace TimerTest
{
public partial class Form1 : Form
{
List<Timer> m_timerList = new List<Timer>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (m_timerList.Count == 0)
{
for (int i = 0; i < 100; i++)
{
Timer timer = new Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
m_timerList.Add(timer);
}
}
else
{
foreach (Timer timer in m_timerList)
{
timer.Tick -= new EventHandler(timer_Tick);
timer.Stop();
timer.Dispose();
}
m_timerList.Clear();
}
}
void timer_Tick(object sender, EventArgs e)
{
}
}
}
Any help?
Thanks,
Guru
Alvin Bruney [MVP] - 11 Jul 2007 02:53 GMT
hmmm, i'd run windbg against it to rule out the managed portion first. Have
you? I do recall a thread in here about a few months ago talking about just
that. I don't remember the solution though

Signature
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
https://www.microsoft.com/MSPress/books/10933.aspx
OWC Black Book www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley
>I am seeing a constant increase in Private bytes of a process that
>continuosly creates timers and disposes them. This increase is in the
[quoted text clipped - 93 lines]
>
> Guru
Bram - 15 Jul 2007 15:16 GMT
Just out of sheer curiosity but why would you want to do this? I'm
having a hard time thinking of a situation which can't be solved using
just one or two timers - surely not 100. Could you elaborate a bit
about the scenario in which you encounter this problem?
Regards
Bram
> I am seeing a constant increase in Private bytes of a process that
> continuosly creates timers and disposes them. This increase is in the
[quoted text clipped - 89 lines]
>
> Guru