简体   繁体   English

如何在GC收集垃圾之前在asp.net中处理DataTable?

[英]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. 我的应用程序24X7工作,我使用DataTable进行数据处理。

I need to dispose the DataTable everytime. 我每次都需要处理DataTable。 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. 我使用了Dispose和Clear方法,但是当我调试代码时,内容只被清除,其实例仍在内存中。

How can I remove it from memory without depending upon the GC? 如何在不依赖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). 如果你用断点检查,你可以看到数据表'dt'在调用dispose()后仍然有值。即使执行第11行仍然存在值。我需要在执行第12行之前释放该内存(或之前)第二次迭代)。 what should i do here? 我该怎么办?

If you set every reference to the datatable to nothing/null the underlying data can be garbage collected. 如果将对数据表的每个引用都设置为nothing / null,则可以对基础数据进行垃圾回收。 When an object is orphaned the dotnet framework will automatically garbage collect when it feels it is appropriate. 当一个对象成为孤儿时,dotnet框架会在感觉合适时自动进行垃圾收集。

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 http://msdn.microsoft.com/en-us/library/xe0c2357.aspx

Summary on how dotnet GC works. 关于dotnet GC如何工作的总结。 http://dotnetfacts.blogspot.com/2008/05/how-garbage-collector-works-part-1.html 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. 如果垃圾收集器没有释放内存,可能是因为您可能编写的其他对象,Ui控件/其他业务对象引用了数据表中的行数据。 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. 如果不依赖GC,则无法将其从内存中删除。 It will depend on GC when and how to free the memory. 它将取决于GC何时以及如何释放内存。 GC works in an indeterministic manner which means you can't be sure when GC will actually free the instance GC以不确定的方式工作,这意味着您无法确定GC何时实际释放实例

You should just make sure you are not keeping any reference of DataTable when it is not needed any more. 您应该确保在不再需要DataTable时不要保留任何参考。 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 如果您已正确完成该部分,即除非有必要,否则没有对DataTable的引用,那么您就是好的并且在GC上保持休息状态

I doubt it is a good design to try rebuid what the GC do anyway. 我怀疑尝试重建GC无论如何都是一个很好的设计。 Why don't you just run a loop task which runs 24/7 that just start the Process as a new thread? 为什么不运行一个24/7运行的循环任务,只是将Process作为一个新线程启动? 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. 垃圾收集将需要完全处理它,但如果您不再需要它,您可以始终将其设置为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块中,而不是调用dispose方法:

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. MSDN文章本身是一个显而易见的资源,如果您之前从未使用它,但这个想法非常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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