[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1216
  • Last Modified:

Possible memory leak in VB.NET application with Timer

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: ftp://accdata1.eurosql.com/ReplicationStatus.zip

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
    CheckClientStatus()
    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

0
treadmill
Asked:
treadmill
2 Solutions
 
vb_jonasCommented:
GC.Collect() ?
0
 
AlexFMCommented:
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.
0
 
treadmillAuthor Commented:
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?
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now