.net net应用程序中的内存泄漏
我正在VB.NET2005中开发一个桌面应用程序。应用程序包含一个间隔为1分钟的计时器。每次计时器滴答作响时,都会执行一组函数,这些函数主要与数据库相关。 最初,应用程序运行良好。在ProcessTask manager中,每次调用计时器时,cpu使用率都会达到100%。但时间跨度约为1秒。 然而,随着时间的推移,大约20小时后,计时器滴答声的时间跨度增加到20-30秒。在此期间,cpu使用率为100%,应用程序不响应。计时器滴答声的时间跨度逐渐增加到1分钟,cpu使用率停留在100%,应用程序不响应。 所有物体都被妥善处理。 此外,奔腾4处理器也存在这个问题。应用程序在core 2 duo上运行良好 计时器包含4个功能。。。我正在添加其中的一些函数.net net应用程序中的内存泄漏,.net,sql-server,vb.net,sql-server-2008,timer,.net,Sql Server,Vb.net,Sql Server 2008,Timer,我正在VB.NET2005中开发一个桌面应用程序。应用程序包含一个间隔为1分钟的计时器。每次计时器滴答作响时,都会执行一组函数,这些函数主要与数据库相关。 最初,应用程序运行良好。在ProcessTask manager中,每次调用计时器时,cpu使用率都会达到100%。但时间跨度约为1秒。 然而,随着时间的推移,大约20小时后,计时器滴答声的时间跨度增加到20-30秒。在此期间,cpu使用率为100%,应用程序不响应。计时器滴答声的时间跨度逐渐增加到1分钟,cpu使用率停留在100%,应用程序
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
许多选择、更新和删除查询在计时器中执行
当我在数据库中使用大约7000条记录时,就会出现这个问题。如果记录较少,则不会出现问题。因此,SQL查询可能是造成这种情况的罪魁祸首
你能告诉我内存泄漏的罪魁祸首是什么吗
期待帮助。提前谢谢 我建议使用一些探查器工具。有一些是免费的,但根据我的经验,这些商业工具是物有所值的 例如ANTS Profiler。
也许你的评估时间足够了?我在这里做一个猜测,因为你提到只有当记录数超过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条记录
希望这有帮助。看看性能计数器,特别是在垃圾收集方面。你分析过你的代码来找出CPU的去向吗?我们能看到一些理论之外的东西吗???有些事情正在发生,但看不见很难知道是什么…我没有使用剖析器。但是我们注意到,计时器中包含多个sql server交互的代码正在产生问题。我关闭了计时器,问题就消失了。您使用的是非托管资源吗?你确定一切都被妥善处理了吗?请发布在勾号事件中运行的代码。@GeekOnDemand发布代码使您更容易获得帮助。
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