C# Datatable.Dispose() 会使其从内存中删除吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18869079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Datatable.Dispose() will make it remove from memory?
提问by Rajeev Kumar
I have researching through very simple code and get stuck on seeing the dispose() result of datatable
我已经研究了非常简单的代码,并被困在查看数据表的 dispose() 结果
Following is the code
以下是代码
DataTable dt= new Datatable();
SqlCommand Cmd = new SqlCommand("sp_getData",SqlCon);
SqlCommand.CommandType= CommandType.StroedProcedure;
SqlCon.Open();
sqlDataReader dr= cmd.ExecuteReader();
dt.Load(dr);
SqlCon.Close();
grdView.DataSource =dt;
dt.Dispose() // Here I dispose the table as it is no use for me & wanna memory free from this
But after disposing off the datatable i still found that it is still showing RowCount = 10k.
但是在处理掉数据表后,我仍然发现它仍然显示 RowCount = 10k。
Does Dispose() method does not free up the memory & make object as null ??
Dispose() 方法不会释放内存并将对象设为 null 吗?
How can i make it as null or free up the memory occupied by this object ??
我怎样才能让它为空或释放这个对象占用的内存?
采纳答案by MoonKnight
DataSet
and DataTable
don't actually have any unmanaged resources, so Dispose()
doesn't actually do much. The Dispose()
methods in DataSet
and DataTable
exists ONLY because of side effect of inheritance - in other words, it doesn't actually do anything useful in the finalization.
DataSet
并且DataTable
实际上没有任何非托管资源,因此Dispose()
实际上并没有做太多事情。Dispose()
inDataSet
和 中的方法DataTable
仅由于继承的副作用而存在 - 换句话说,它实际上在最终确定中没有做任何有用的事情。
It turns out that DataSets
, DataViews
, DataTables
suppress finalization in their constructorsc this is why calling Dispose()
on them explicitly does nothing.
事实证明,DataSets
, DataViews
,DataTables
在其构造函数中抑制终结,这就是为什么Dispose()
显式调用它们什么都不做的原因。
Presumably, this happens because, as mentioned above, they don't have unmanaged resources; so despite the fact that MarshalByValueComponent
makes allowances for unmanaged resources, these particular implementations don't have the need and can therefore forgo finalization.
据推测,发生这种情况是因为,如上所述,他们没有非托管资源;因此,尽管MarshalByValueComponent
允许非托管资源,但这些特定实现没有必要,因此可以放弃最终确定。
Overview of this Immense Answer:
这个巨大的答案的概述:
Without a doubt, Dispose should be called on any Finalizable objects.
DataTables are Finalizable.
Calling Dispose significantly speeds up the reclaiming of memory.
MarshalByValueComponent
callsGC.SuppressFinalize(this)
in itsDispose()
- skipping this means having to wait for dozens if not hundreds ofGen0
collections before memory is reclaimed.
毫无疑问,应该在任何 Finalizable 对象上调用 Dispose。
数据表是可终结的。
调用 Dispose 显着加快了内存的回收。
MarshalByValueComponent
调用GC.SuppressFinalize(this)
它Dispose()
- 跳过这意味着在Gen0
回收内存之前必须等待数十个甚至数百个集合。
Further Reading:
进一步阅读:
See this questionand the related answer.
回答by Adam Houldsworth
Does Dispose() method does not free up the memory & make object as null ??
Dispose() 方法不会释放内存并将对象设为 null 吗?
Dispose
and the disposal pattern is not for reclaiming managed memory or "deleting" managed objects (both things you cannot do and what the Garbage Collector is there for), it is for handling the disposal/release of unmanaged resources or other managed resources that have releasable items, such as SqlConnection
. It certainlywon't null
the reference, but may make it unusable from the time of disposal forwards.
Dispose
并且处置模式不是用于回收托管内存或“删除”托管对象(您不能做的事情以及垃圾收集器的用途),它用于处理非托管资源或其他可释放托管资源的处置/释放项,例如SqlConnection
。它当然不会null
作为参考,但可能会使其从处置时起无法使用。
How can i make it as null or free up the memory occupied by this object ??
我怎样才能让它为空或释放这个对象占用的内存?
If you want to null the reference, simply dt = null
will work, though this will notgive you any benefit as the DataTable
instance is referenced by grdView.DataSource
. Both dt
and grdView.DataSource
will be references to the same underlying DataTable
instance.
如果您想将引用清空,则dt = null
可以正常工作,尽管这不会给您带来任何好处,因为DataTable
实例是由grdView.DataSource
. 双方dt
并grdView.DataSource
会以同一个基础的参考DataTable
实例。
I also suspect this is part of a method in which case dt
is method-scoped anyway.
我也怀疑这是方法的一部分,在这种情况下无论如何dt
都是方法范围的。
You shouldn't have to worry too much about any of this stuff. I'd be more concerned about having the SqlConnection
outside of a try-finally
/ using
, you are at risk of leaving a connection open there.
你不必太担心这些东西。我会更关心/的SqlConnection
外部,你有在那里打开连接的风险。try-finally
using
I tend to favour calling Dispose
on items that implement IDisposable
for what I think is a verygood reason: this is the public contract. The fact of whether calling it does anything or not is an implementation detailand is liable to changeat a moments notice.
我倾向于赞成呼吁Dispose
在实现项目IDisposable
什么,我认为这是一个非常好的理由:这是公共合同。调用它是否有任何作用的事实是一个实现细节,并且可能会随时更改。
顺便说一句,我会完全重写您的代码:
var dt = new Datatable();
using (var conn = new SqlConnection(""))
using (var comm = new SqlCommand("sp_getData", conn))
{
conn.Open();
using (var reader = comm.ExecuteReader())
{
dt.Load(reader);
}
}
grdView.DataSource = dt;
回答by Hasan Savran
Try to use Clear() function. It works great for me for disposing.
尝试使用 Clear() 函数。它对我来说非常适合处理。
DataTable dt = GetDataSchema();
//populate dt, do whatever...
dt.Clear();