2016-12-28 79 views
0

我已经使用.net 4.0编写了vb中的URL监视程序。基本上它设置一个计时器使用htpwebreques/httpwebresponse每60分钟检查一次URL,并在URL关闭时发送电子邮件。但是,每次检查url时,应用程序使用的内存都会不断增加。这显然最终会导致一个问题,因为该应用程序旨在永久监控网站的可用性,并且监控机器最终将耗尽资源。URL监视器不断增加内存使用量

下面是我的CheckURL例程的代码。任何建议非常感谢,提前感谢。

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 
+2

在'Using'语句中换行请求,因为这样可以确保您的对象被丢弃。现在他们没有被处置。 – Codexer

+0

@Zaggler谢谢,但我试图添加一个使用语句,但得到错误“变量'wRes'隐藏一个变量在一个封闭的块和StatusCode不是WebResponse的成员” – omicron

回答

0

首先,你应该使用Option Strict On,它会告诉你,你有变量的类型不匹配,甚至可能会建议更正你,例如,看到那里的DirectCast运营商在下面的代码中使用。

其次,HttpWebResponse.Dispose()方法,所以你应该调用使用,当你完成它,或者像Zaggler指出的那样,你可以使用Using,以确保非托管资源被正确地清理,从而消除了内存泄漏你很关心。请注意,代码中可能存在其他类似问题,我们无法看到。

你应该不是设置为Nothing试图摆脱它们 - 这样做与垃圾收集器混乱,并没有做任何事情,以确保其清洁处置。

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 
+0

我已经实现了上面的代码使用块,但我仍然获得持续的记忆力增加。作为一个测试,我在End Using之后添加了一个gc.collect()语句,然后内存使用保持稳定。但我明白这不是一个好习惯。任何想法可能是错误的,就像垃圾回收器在处理对象后不会释放资源。 – omicron

+0

这可能是程序内存会增加到一定的数量和水平,或者垃圾收集器可能会做更多,如果计算机内存压力。你在程序的其他地方创建了很多字符串吗? - 如果是这样,['StringBuilder'](https://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v = vs.110).aspx)可以产生很大的差异。 –