Possible memory leak in VB.NET application with Timer

Posted on 2006-06-06
Last Modified: 2008-03-10
Hi All,

I wonder if someone can have a look at my code and tell me if there is a possible memory leak.  You can download the VS 2003 project from here:

Basically, when the app is started it uses approx 1.6MB of memory, during a short period, say about 10 minutes, the useage seems to settle around 25MB!

A little background on what the app does...

The application is a taskbar app and has a very small windows form, which when opened, shows the status of a clients SQL Server pull subscription.  The taskbar icon has different states depending on the status of the pull subscription, which is updated every 5 seconds.  The status on the windows form itself is not updated, until the user selects to show the windows form.

Below is the timer code which fires-off this status update request.  If I disabled this timer, the app memory stays at 1.6MB, so obviously it's what the sub CheckClientStatus() is doing that is the problem, but I haven't managed to work out what that is.

Have a look at the ReplStatus.vb form and code and look for the following timer and below this the culprit sub CheckClientStatus()

  Private Sub myTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myTimer.Tick
    myTimer.Interval = CInt(RefreshInterval.Text) * 1000
    myTimer.Enabled = False
    myTimer.Enabled = True
  End Sub

I'm sure this is a clean-up problem, but I cannot see what I can do to my code to fix this.  Interestingly, when I select from the the taskbar icon to open the small windows form, to see a full description of the status, upon clicking "OK" which minimises and hides the form, the memory useage then drops back down to 1.6MB.  Obviously .NET is finally doing it's own clean-up processes, but this is the only time this will happen.

The reason for the 500 points is more for urgency. ;)

Many thanks,

Kevin Smith

Question by:treadmill
    LVL 12

    Accepted Solution

    GC.Collect() ?
    LVL 48

    Assisted Solution

    Your application doesn't use unmanaged code, memory leaks in pure .NET code are impossible (unless there is some bug in .NET Framework). .NET Grabage Collector allows to application to consume almost all available memory before it decides to make collection. I think your code is OK.
    LVL 1

    Author Comment

    vb_jonas, using GC.Collect(), after the timer event has elapsed, appears to limit the amount of RAM consumed.  It sits, so far, at about 7.2MB useage. I will keep an eye on this for a little while longer, as it normally takes about 10 minutes or so before it consumes 25MB.

    AlexFM, thanks for this information about the GC and .NET's use of memory. Both of your comments have helped solved this problem, as you have made me aware of what .NET will do with memory and how to clear it up, rather than wait for .NET to do it for me.

    Now, how do I equally award points to both of you?  Do I accept both of your answers?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now