简体   繁体   中英

How to dispose DataTable in asp.net before garbage collection by the GC?

My application is working 24X7 and I use DataTable for data handling.

I need to dispose the DataTable everytime. I used Dispose and Clear methods, but the content is only cleared and its instance is still in the memory when I debugged the code.

How can I remove it from memory without depending upon the GC?

Thanks in advance..

EDIT: This is my code prototype . i have done it in two ways.

 1     //Methord 1
 2               while (true)
 3               {
 4                   DataTable dt = new DataTable();
 5                   dt.Columns.Add("City", typeof(string));
 6                   dt.Rows.Add("ABC");
 7                   dt.Rows.Add("XYZ");
 8                   dt.Rows.Add("PQR");
 9                   dt.Rows.Add("LMN");
 10                   dt.Dispose();
 11               }
 12    //Methord 2
 13               while (true)
 14               {
 15                   using (DataTable dtable = new DataTable())
 16                   {
 17                       dtable.Columns.Add("City", typeof(string));
 18                       dtable.Rows.Add("ABC");
 19                       dtable.Rows.Add("XYZ");
 20                       dtable.Rows.Add("PQR");
 21                       dtable.Rows.Add("LMN");
 22                   }
 23               }

how will the datatable gets disposed ? if you check with breakpoint , you can stil see datatable 'dt' still has values even after calling dispose().even on execution of Line No 11 values are still there.i need to free that memory before execution of line 12 (or before 2nd iteration of while). what should i do here?

If you set every reference to the datatable to nothing/null the underlying data can be garbage collected. When an object is orphaned the dotnet framework will automatically garbage collect when it feels it is appropriate.

You can tell the garbage collector to collect manually, but it will only collect against objects that don't have a reference in code. http://msdn.microsoft.com/en-us/library/xe0c2357.aspx

Summary on how dotnet GC works. http://dotnetfacts.blogspot.com/2008/05/how-garbage-collector-works-part-1.html

If the Garbage collector isn't freeing up the memory, it probably because other objects,Ui Controls/Other business objects you might have written have references to row data in your datatable. For example you are displaying row data, or you have pass a row into another object and it has a reference to this row.

If your experiencing ever increasing memory, you have a memory leak, ie you have an ever increasing number of objects that have references to them and the framework cant garbage collect this data because it thinks it is being used. You will need to look at your code and try and figure out why it is leaking, a memory profiler may help.

You cannot remove it from memory without depending on GC. It will depend on GC when and how to free the memory. GC works in an indeterministic manner which means you can't be sure when GC will actually free the instance

You should just make sure you are not keeping any reference of DataTable when it is not needed any more. If you have done that part correctly ie there is no reference to DataTable unless required then you are good and leave rest on GC

I doubt it is a good design to try rebuid what the GC do anyway. Why don't you just run a loop task which runs 24/7 that just start the Process as a new thread? When the thread is done you can destroy it and in result you don't have a memory problem.

Garbage collection will be required to completely dispose of it, but if you no longer need it you can always set it to null.

//do some stuff with the table

//don't need it any longer so set it null
table = null;

Instead of calling the dispose method, you could always wrap the datatable itself in a using block:

using (DataTable dt = new DataTable())
{
   //Do work here
}

This would insure that it is cleaned up when the object itself goes outside of its scope.

The MSDN Article itself is an obvious resource if you haven't ever used it before, but the idea is very simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM