Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Performance / July 2007

Tip: Looking for answers? Try searching our database.

Memory leak when using System.Windows.Forms.Timer?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Guru - 09 Jul 2007 19:04 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.