Datatable.Dispose () will delete it from memory?

I have research through very simple code and am stuck looking for dispose () result from datatable

Below 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 deleting the datatable data, I still found that it still shows RowCount = 10k.

The Dispose () method does not free memory and makes the object null.

How can I make it null or free the memory occupied by this object?

+6
source share
3 answers

DataSet and DataTable actually have no unmanaged resources, so Dispose() actually does little. The Dispose() methods in the DataSet and DataTable exist ONLY because of the side effect of inheritance - in other words, it actually does nothing useful in terminating.

It turns out that DataSets , DataViews , DataTables suppress completion in their constructors. This is why calling Dispose() on them obviously does nothing.

Presumably, this is because, as mentioned above, they do not have unmanaged resources; therefore, although the MarshalByValueComponent makes assumptions for unmanaged resources, these specific implementations do not need this and therefore may refuse to complete.

Overview of this huge answer :

No doubt Dispose should be called for any Finalizable objects.

The data tables are complete.

Calling Dispose significantly speeds up memory recovery.

MarshalByValueComponent calls GC.SuppressFinalize(this) in its Dispose() skip, which means waiting for dozens, if not hundreds of Gen0 collections, before memory is restored.

Further reading:

See this question and the corresponding answer.

+14
source

Does the Dispose () method free memory and make the object null?

Dispose and the delete pattern is not intended to restore managed memory or β€œdelete” managed objects (both things you cannot do and what a garbage collector is), it is designed to handle the removal / release of unmanaged resources or other managed resources that have released items such as SqlConnection . This, of course, will not be a null reference, but may make it unusable from the moment of removal forward.

How can I do this as null or free the memory occupied by this object

If you want to remove the link, just dt = null will work, although this one will not give you any benefit, since the DataTable instance refers to grdView.DataSource . Both dt and grdView.DataSource will be references to the same underlying instance of the DataTable .

I also suspect that this is part of the method, in which case dt in any case be the scope of the method.

You do not need to worry too much about it. I'm more worried about having SqlConnection outside of try-finally / using , you run the risk of leaving an open connection there.

I prefer to call Dispose on elements that implement IDisposable , because I think this is a very good reason: this is a public contract . The fact that calling him does something or not is an implementation detail and may change at the time of notification.


As an aside, I would completely rewrite 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; 
+6
source

Try using the Clear () function. This works great for me for recycling.

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

Source: https://habr.com/ru/post/954051/


All Articles