|
-
Feb 27th, 2006, 07:48 PM
#1
Thread Starter
Hyperactive Member
Memory leak in the VB.NET ping routine?
I've got a huge memory leak in my multi-threaded application that eventually crashes the program and bogs down the computer when it has exhausted my 1.5GB of RAM! I have about 15 threads running all at the same time. Each thread scans one of our subnets, for example, from 10.10.10.1 to 10.10.10.149 to find an active Win2k or WinXP computer. If it finds a computer it uses WMI to interface and pull a bunch of system and user data. Since WMI takes 60 seconds to timeout I ping each IP address before attempting to use WMI on it. I'm using a simple ping routine about 2250 times per cycle. Once a cycle finishes the thread sleeps for 5-7 minutes and starts again.
This is the ping routine code.
VB Code:
Private Function Ping(ByVal Computer As String) As Boolean
Try
If My.Computer.Network.Ping(Computer) Then
Ping = True
Else
Ping = False
End If
Catch ex As Exception
Ping = False
End Try
End Function
In trying to diagnose the problem I stumbled across this page and wonder if the problem would apply to my VB.NET program as well? I'm not experienced enough yet to understand all of the terminology, but can see that the Garbage Collector has trouble dealing with the ping functionality. So, a bunch of ping objects get built up in memory and are never released. I've tried calling the GC mulitple times throughout code, setting objects that are going out of scope to nothing, etc. I'm hoping someone can take a look and let me know what they think and if I'm affected.
http://blogs.msdn.com/joncole/archiv...15/505627.aspx
I'm testing my program now by completly removing the ping routine. After about an hour so far, the memory usage has barely moved... sitting right around 24MB. Granted it is going to take longer for there to be any evidence of a leak since the WMI timeout is ~120 times longer to return than a simple ping. I'm going to let it run overnight if possible and see what happens.
VB.NET 2005 Express with .Net 2.0
C# 2010 .Net 4.0
-
Feb 27th, 2006, 08:08 PM
#2
Re: Memory leak in the VB.NET ping routine?
I would guess that you are affected. My guess would be that the Ping method that you're using creates a Ping object and calls Dispose without the cast suggested in that article. The thing to do would be to reimplement your Ping method and explicitly create and destroy a Ping object rather than using very handy but apparently flawed My.Computer.Network.Ping method. You would create and destroy the Ping object like this:
VB Code:
Dim myPing As New Ping 'Create the Ping object.
DirectCast(myPing, IDisposable).Dispose() 'Cast the Ping object as IDisposable and destroy it.
You could try the code converter in my signature to convert the code in the article to VB.NET, although keep in mind that it's not 100% accurate. You may need to make some manual adjustments afterwards.
-
Feb 27th, 2006, 08:40 PM
#3
Thread Starter
Hyperactive Member
Re: Memory leak in the VB.NET ping routine?
WOW! What a difference that made. I created a persistent ping object that I just keep reusing. The memory consumption has dropped WAY down. I'm going to try using the Dispose method at the end of a run before putting the thread to sleep as well now, and see what that nets me.
VB.NET 2005 Express with .Net 2.0
C# 2010 .Net 4.0
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|