URL monitor keeps increasing memory usage

100 Views Asked by At

I have written a URL monitoring program in vb using .net 4.0. Basically it sets a timer checks the url every 60 minutes using an htpwebreques/httpwebresponse and sends an email if the url is down. However the memory used by the application keeps increasing every time the url is checked. This will obviously eventually cause a problem as the app is designed to run permanently monitoring a website for availability and the monitoring machine will eventually run out of resources.

Code for my CheckURL routine below. Any advice greatly appreciated, thanks in advance.

    Private Sub checkURL()
    Timer1.Stop()
    Dim wReq As HttpWebRequest
    Dim wResp As HttpWebResponse ' WebResponse

    wReq = HttpWebRequest.Create(url)
    wReq.Method = "HEAD"
    Try
        wResp = wReq.GetResponse()
        If wResp.StatusCode = 200 Then
                txtResponse.Text = wResp.StatusCode & ": " & wResp.StatusDescription & vbNewLine & "The " & siteName & " is up"

                'Only send success results if specified
                If sendOnFailure = False Then
                    sendResults = True
                End If
            Else txtResponse.Text = "There may be a problem with the " & siteName & vbNewLine & "Please verify manually that it is operational." & vbNewLine & "The response received was:" & vbNewLine & "Status Code: " & wResp.StatusCode & " - " & wResp.StatusDescription
                sendResults = True
            End If

        wResp.Close()
        wResp = Nothing
        wReq = Nothing

    Catch ex As Exception
            txtResponse.Text = "There may be a problem with the " & siteName & vbNewLine & "The error returned was:" & vbNewLine & ex.ToString
            sendResults = True

    End Try

    txtLastCheck.Text = Now.ToString("d MMM yyyy HH:mm")
    setNextCheck()

End Sub
1

There are 1 best solutions below

2
On BEST ANSWER

First, you should use Option Strict On, which will show you where you have variable type mismatches and may even suggest corrections for you, for example, see where the DirectCast operator is used in the following code.

Second, HttpWebResponse has a .Dispose() method, so you should call that when you have finished using it, or, as Zaggler pointed out, you can use Using to ensure that unmanaged resources are cleaned up properly, thus removing the memory leak you are concerned with. Note that there may be other similar problems in the code we can't see.

You should not set things to Nothing in an attempt to get rid of them - doing so messes with the garbage collector and does nothing to ensure their clean disposal.

Option Strict On
' ....

Private Sub checkURL()
    timer1.Stop()
    Dim wReq As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
    wReq.Method = "HEAD"

    Try
        Using wResp As HttpWebResponse = DirectCast(wReq.GetResponse(), HttpWebResponse)

            If wResp.StatusCode = 200 Then
                txtResponse.Text = wResp.StatusCode & ": " & wResp.StatusDescription & vbNewLine & "The " & siteName & " is up"

                'Only send success results if specified
                If sendOnFailure = False Then
                    sendResults = True
                End If
            Else txtResponse.Text = "There may be a problem with the " & siteName & vbNewLine & "Please verify manually that it is operational." & vbNewLine & "The response received was:" & vbNewLine & "Status Code: " & wResp.StatusCode & " - " & wResp.StatusDescription
                sendResults = True
            End If

            wResp.Close()
        End Using

    Catch ex As Exception
        txtResponse.Text = "There may be a problem with the " & siteName & vbNewLine & "The error returned was:" & vbNewLine & ex.ToString
        sendResults = True

    End Try

    txtLastCheck.Text = Now.ToString("d MMM yyyy HH:mm")
    setNextCheck()

End Sub