.net应用程序中的内存泄漏

时间:2011-10-19 13:31:59

标签: .net sql-server vb.net sql-server-2008 timer

我正在使用VB.net 2005中的桌面应用程序。该应用程序包含一个间隔为1分钟的计时器。每次计时器滴答时,都会执行一组函数,主要是数据库相关的。 最初应用程序运行正常。在进程(任务管理器)中,每次调用计时器时,cpu使用率都会达到100%。但是时间跨度大约是1秒(可以忽略不计)。 然而,随着时间的推移,大约20小时后,timer_tick的时间跨度增加到20-30秒。在此期间,CPU使用率为100%,应用程序无响应。渐渐地,timer_tick的时间跨度增加到1分钟,并且cpu使用被卡住了100%并且应用程序没有响应。 所有物体都妥善处理。 此外,这个问题与奔腾4处理器有关。应用程序在core 2 duo上运行良好。

计时器包含4个功能......我正在添加其中一些功能..

 Public Sub SetNotes()
    Dim dtOld As DataTable
    Dim dtNew As DataTable
    Dim oApptTasks As New AppointmentsAndTasks

    dtOld = oApptTasks.PopulateAllTasks  ' get the source table
    dtNew = dtOld.Clone  '  make new table ad clone of old table

    If btnShowNotes.Text = "Hide Notes" Then
        For Each item As System.Data.DataRow In dtOld.Rows
            If Not IsDBNull(item("Notes")) Then
                If item("Notes") <> "" Then ' add only if not null and not blank
                    item("Task") = item("Task") & vbCrLf & item("Notes") ' concatenate the notes field
                End If
            End If
            dtNew.ImportRow(item) ' import modified row to new table
        Next

        grdcTask.DataSource = SetAssignedTo(dtNew) ' set the datasource
        grdcTask.DataSource = SetAssignedFrom(grdcTask.DataSource) ' set the datasource
        repMemoNotes.LinesCount = 0 ' adjust the height of custom field
    Else
        grdcTask.DataSource = SetAssignedTo(dtOld) ' set the datasource
        grdcTask.DataSource = SetAssignedFrom(grdcTask.DataSource) ' set the datasource
    End If
End Sub

现在这是timer定义的四个函数之一...它使用以下代码从db中获取数据。

  Using conn As New SqlConnection(glbSqlConnString)
        Try
            conn.Open()
            Dim dbDataAdapter As New SqlDataAdapter(oStrQueryBuilder.ToString, conn)
            dbDataAdapter.Fill(dbDataTable)

        Catch ex As Exception
            EventLog.WriteLog("App", ex.ToString, EventLogEntryType.Error)
        Finally
            If conn.State = ConnectionState.Open Then
                conn.Close()
            End If
        End Try
    End Using

许多选择,更新和删除查询都在计时器中执行。

当我在数据库中使用大约7000条记录时会发生此问题。记录越少,问题就不会发生。因此,SQL查询是否可能成为此问题的罪魁祸首。

你能否说一下内存泄漏的罪魁祸首?

期待寻求帮助。提前致谢。

2 个答案:

答案 0 :(得分:0)

我建议使用一些Profiler工具。有一些免费的,但根据我的经验,商业工具是物有所值的。

例如ANTS Profiler。 http://www.red-gate.com/products/dotnet-development/ Perphaps评估perios对你来说足够了吗?

答案 1 :(得分:0)

我在这里猜测,因为你已经提到只有当记录数超过7000时才会出现问题;它可能是在上一个事件完成其数据库操作之前触发了下一个计时器触发器的情况。这可能会导致需要完成的积压操作,并且随着时间的推移它会不断增加。

我建议您确保在任何给定时间只运行一组操作。例如,使用以下逻辑运行计时器功能

Function TimerFunction
     'Diable Timer - This will disable the timer so that no more timer events are triggered'
     timer1.Enabled = False


     Function1()
     Function2()
     Function3()
     Function4()

     'Enable Timer - Now enable the timer again so that it can continue normally'
     timer1.Enabled=True

End

但是您确定需要对数据库操作进行概要分析并调整延迟执行程序的操作。值得关注的是,您的应用程序性能开始落后于仅有7000条记录。

希望有所帮助。

相关问题