URL 监视器不断增加内存使用量
URL monitor keeps increasing memory usage
我使用 .net 4.0 在 vb 中编写了一个 URL 监控程序。基本上,它设置了一个计时器,每 60 分钟检查一次 url,使用 htpwebreques/httpwebresponse 并在 url 关闭时发送电子邮件。但是,每次检查 url 时,应用程序使用的内存都会不断增加。这显然最终会导致问题,因为该应用程序旨在 运行 永久监控网站的可用性,并且监控机器最终会 运行 资源不足。
下面是我的检查URL例程的代码。非常感谢任何建议,提前致谢。
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
首先,您应该使用 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
运算符,用于以下代码。
其次,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
确保正确清理非托管资源,从而消除您担心的内存泄漏。注意,代码中可能还有其他类似的问题我们看不到。
您应该 不 将事物设置为 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
我使用 .net 4.0 在 vb 中编写了一个 URL 监控程序。基本上,它设置了一个计时器,每 60 分钟检查一次 url,使用 htpwebreques/httpwebresponse 并在 url 关闭时发送电子邮件。但是,每次检查 url 时,应用程序使用的内存都会不断增加。这显然最终会导致问题,因为该应用程序旨在 运行 永久监控网站的可用性,并且监控机器最终会 运行 资源不足。
下面是我的检查URL例程的代码。非常感谢任何建议,提前致谢。
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
首先,您应该使用 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
运算符,用于以下代码。
其次,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
确保正确清理非托管资源,从而消除您担心的内存泄漏。注意,代码中可能还有其他类似的问题我们看不到。
您应该 不 将事物设置为 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