Vb.net URL监视器不断提高内存使用率
我已经在vb中使用.NET4.0编写了一个URL监控程序。基本上,它会设置一个计时器,使用htpWebRequests/httpwebresponse每隔60分钟检查一次url,如果url关闭,它会发送一封电子邮件。但是,每次检查url时,应用程序使用的内存都会不断增加。这显然最终会导致一个问题,因为该应用程序被设计为永久性地监控网站的可用性,而监控机器最终将耗尽资源 下面是我的CheckURL例程的代码。非常感谢您的建议,提前谢谢Vb.net URL监视器不断提高内存使用率,vb.net,.net-4.0,Vb.net,.net 4.0,我已经在vb中使用.NET4.0编写了一个URL监控程序。基本上,它会设置一个计时器,使用htpWebRequests/httpwebresponse每隔60分钟检查一次url,如果url关闭,它会发送一封电子邮件。但是,每次检查url时,应用程序使用的内存都会不断增加。这显然最终会导致一个问题,因为该应用程序被设计为永久性地监控网站的可用性,而监控机器最终将耗尽资源 下面是我的CheckURL例程的代码。非常感谢您的建议,提前谢谢 Private Sub 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
首先,您应该使用,它将向您显示变量类型不匹配的位置,甚至可能为您建议更正,例如,请参阅以下代码中运算符的使用位置
其次,它有一个.Dispose()
方法,因此您应该在使用完它后调用它,或者正如Zaggler所指出的,您可以使用它来确保正确清理非托管资源,从而消除您所关心的内存泄漏。请注意,代码中可能存在我们看不到的其他类似问题
您不应该试图将事物设置为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
使用语句在
中包装请求,这样可以确保对象得到处置。现在它们还没有被处理。@Zaggler谢谢,但我尝试添加了一个using语句,但出现了错误“变量'wRes'在封闭块中隐藏了一个变量,而StatusCode不是WebResponse的成员”我已经实现了上述代码中的using块,但仍在不断增加内存。作为测试,我在结束使用后添加了一个gc.collect()语句,然后内存使用保持稳定。但我知道这不是一个好的做法。有什么想法可能是错误的吗?就像垃圾收集器在处理对象后没有释放资源一样。可能是程序内存会增加到一定数量并趋于平稳,或者如果计算机内存不足,垃圾收集器可能会做得更多。您是否在程序的其他地方创建了大量字符串?-如果是这样的话,a可以带来很大的不同。