I have created a VB.Net service that uses WMI to check my network card statistics every 5 minutes using a Timer and posting that info to a ASP page. What I find is that the service as listed in Task Manager keeps going up in memory. I am not the best coder out there, can someone see a problem in my code or let me know if this is by design?
Private Sub Timer1_Elapsed_1(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEvent
Args) Handles TimerSendNICInfo.Elapsed
'Record log about starting service timed event
Me.LogInfo("Send NIC Info at " & Now.ToLongDateString & " - " & Now.ToShortTimeString)
' Set initial variables and WMI object
Dim oWMI
Dim oTmp
Dim oCtrl
Dim web As New System.Net.WebClient
Dim sServerNetworkDetails
Dim strPostNetworkData As String
Dim bytPostNetworkData() As Byte
Dim sComputerName
Dim sNetworkCardName
Dim sBytesReceivedPerSec
Dim sBytesSentPerSec
Dim sBytesTotalPerSec
oWMI = GetObject("winmgmts:")
oTmp = oWMI.InstancesOf("win32_co
mputersyst
em")
For Each oCtrl In oTmp
sComputerName = oCtrl.Caption
Next
oTmp = Nothing
oWMI = Nothing
oCtrl = Nothing
Try
Dim connection As New ConnectionOptions
Dim scope As New ManagementScope( _
"\\.\root\CIMV2", connection)
scope.Connect()
Dim query As New ObjectQuery( _
"SELECT * FROM Win32_PerfFormattedData_Tc
pip_Networ
kInterface
")
Dim searcher As New ManagementObjectSearcher(s
cope, query)
For Each queryObj As ManagementObject In searcher.Get()
sNetworkCardName = queryObj("Name")
sBytesReceivedPerSec = Convert.ToString(queryObj(
"BytesRece
ivedPerSec
"))
sBytesSentPerSec = Convert.ToString(queryObj(
"BytesSent
PerSec"))
sBytesTotalPerSec = Convert.ToString(queryObj(
"BytesTota
lPerSec"))
sServerNetworkDetails = "NetworkCardName=" & sNetworkCardName & "&BytesReceivedPerSec=" & sBytesReceivedPerSec & "&BytesSentPerSec=" & sBytesSentPerSec & "&BytesTotalPerSec=" & sBytesTotalPerSec
'Make the Post Data String
strPostNetworkData = "ComputerNameD=" & sComputerName & "&" & sServerNetworkDetails
web.Headers.Add("Content-T
ype", "application/x-www-form-ur
lencoded")
bytPostNetworkData = System.Text.Encoding.ASCII
.GetBytes(
strPostNet
workData)
web.UploadData("
http://www.mywebsite.com/Network.asp", "POST", bytPostNetworkData)
Next
connection = Nothing
scope = Nothing
query = Nothing
searcher = Nothing
searcher.Dispose()
Catch err As ManagementException
Me.LogInfo("An error occurred while querying for WMI data: " & err.Message)
Catch unauthorizedErr As System.UnauthorizedAccessE
xception
Me.LogInfo("Connection error (user name or password might be incorrect): " & unauthorizedErr.Message)
End Try
' CleanUp
oTmp = Nothing
oWMI = Nothing
oCtrl = Nothing
web = Nothing
sServerNetworkDetails = Nothing
strPostNetworkData = Nothing
bytPostNetworkData = Nothing
sComputerName = Nothing
sNetworkCardName = Nothing
sBytesReceivedPerSec = Nothing
sBytesSentPerSec = Nothing
sBytesTotalPerSec = Nothing
web.Dispose()
End Sub
Start Free Trial