我有一個windows服務,每10秒鐘定期檢查一次數據庫,檢索尚未發送的郵件並執行待處理郵件的任務。使用帶定時器和sql server的windows服務時發生內存泄露
問題是內存一步一步增加。如果需要調整,請幫助。
我的代碼的Visual Basic:
Private Sub TimerMail_Elapsed(sender As Object, e As Timers.ElapsedEventArgs) Handles TimerMail.Elapsed
Dim SqlConnection As New SqlConnection("Data Source=.;Initial Catalog=xxx;Integrated Security=True")
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText = "SELECT DISTINCT UserName, objet, contenu, email FROM message WHERE envoimail='false'"
SqlConnection.Open()
cmd.Connection = SqlConnection
Dim monReader As SqlDataReader = cmd.ExecuteReader()
While monReader.Read
Try
EnvoiMail(monReader("objet"), monReader("contenu"), monReader("email"), mailport, serveur, username, password, True)
Dim SqlConnection2 As New SqlConnection("Data Source=.;Initial Catalog=CGP;Integrated Security=True")
Dim cmd2 As New System.Data.SqlClient.SqlCommand
cmd2.CommandType = System.Data.CommandType.Text
cmd2.CommandText = "UPDATE destinataire SET envoimail='true' WHERE UserName='" & monReader("UserName") & "'"
SqlConnection2.Open()
cmd2.Connection = SqlConnection2
cmd2.ExecuteNonQuery()
Catch ex As Exception
logger.Error(ex.Message)
End Try
Threading.Thread.Sleep(60000)
End While
End Sub