Datatable.Dispose() will make it remove from memory?

故事扮演 提交于 2019-11-26 21:49:14

问题


I have researching through very simple code and get stuck on seeing the dispose() result of datatable

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.

Does Dispose() method does not free up the memory & make object as null ??

How can i make it as null or free up the memory occupied by this object ??


回答1:


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.

It turns out that DataSets, DataViews, DataTables suppress finalization in their constructorsc this is why calling Dispose() on them explicitly does nothing.

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.

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 calls GC.SuppressFinalize(this) in its Dispose() - skipping this means having to wait for dozens if not hundreds of Gen0 collections before memory is reclaimed.

Further Reading:

See this question and the related answer.




回答2:


Does Dispose() method does not free up the memory & make object as 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 certainly won't null the reference, but may make it unusable from the time of disposal forwards.

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 not give 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.

I also suspect this is part of a method in which case dt is method-scoped anyway.

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.

I tend to favour calling Dispose on items that implement IDisposable for what I think is a very good reason: this is the public contract. The fact of whether calling it does anything or not is an implementation detail and is liable to change at a moments notice.


As an aside, I would completely re-write your code:
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;



回答3:


Try to use Clear() function. It works great for me for disposing.

DataTable dt = GetDataSchema();
//populate dt, do whatever...
dt.Clear();


来源:https://stackoverflow.com/questions/18869079/datatable-dispose-will-make-it-remove-from-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!