ASP.Net/SQL:取消查询后内存不会减少(DataBoundControl.PerformSelect)

时间:2011-02-23 14:01:33

标签: c# asp.net sql-server-2005 ado.net memory-leaks

在asp.net 2.0生产环境中,我们有一个aspx,可以查询SQL2005数据库的各种查询。有些查询需要一段时间,所以我们添加了一个取消按钮。

在代码中我们打开连接,然后调用command.ExecuteReader()。我们将它绑定到gridview。

在取消按钮中单击我们调用command.Cancel(),配置命令,连接和datareader。并关闭连接。

执行代码后,我在SQL分析器中看到查询被取消。但是w3wp.exe仍在使用大量内存。

为了测试它,我编写了一个返回大量数据的查询。在2秒内我取消它,我看到的是内存从250Mb增加到几乎2Gb。一段时间后,内存不会减少。

我使用JetBrains dotTrace Memory 3.5来查看内存发生了什么。我看到的是DataBoundControl.PerformSelect正在使用66.6%的内存。微软称:“在检索数据后,会调用PerformDataBinding方法,将数据绑定到数据绑定控件的元素。”但并非所有数据都被检索,呼叫被取消。会发生什么以及为什么内存没有被释放?

任何人都知道发生了什么或/和如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

仅仅因为您处置资源并不意味着框架将回馈内存。实际上,框架的服务器版本将保留分配的内存空间以供以后重用。

How the garbage collection works in .NET

A very useful blog for understanding the internals of garbage collection and performance

From the same blog, see the block after the third note.