如何在GC进行垃圾回收之前处理asp.net中的DataTable?
我的应用程序 24X7 运行,我使用 DataTable 进行数据处理。
我每次都需要处理DataTable。我使用了Dispose和Clear方法,但是当我调试代码时,内容只是被清除了,它的实例仍然在内存中。
如何在不依赖 GC 的情况下将其从内存中删除?
预先感谢..
编辑: 这是我的代码原型。我用两种方式做到了。
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 }
数据表将如何处理?如果您使用断点进行检查,即使在调用 dispose() 之后,您仍然可以看到数据表“dt”仍然具有值。即使在执行第 11 行时,值仍然存在。我需要在执行第 12 行之前(或之前)释放该内存while 的第二次迭代)。我应该在这里做什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果将对数据表的每个引用设置为无/空,则底层数据可以被垃圾收集。当一个对象成为孤立对象时,dotnet 框架会在认为合适时自动进行垃圾收集。
您可以告诉垃圾收集器手动收集,但它只会收集代码中没有引用的对象。
http://msdn.microsoft.com/en-us/library/xe0c2357.aspx
关于 dotnet GC 工作原理的总结。
http://dotnetfacts.blogspot.com/2008 /05/how-garbage-collector-works-part-1.html
如果垃圾收集器没有释放内存,可能是因为其他对象,Ui您可能编写的控件/其他业务对象引用了数据表中的行数据。例如,您正在显示行数据,或者您已将一行传递到另一个对象中,并且它具有对此行的引用。
如果您的内存不断增加,则存在内存泄漏,即引用它们的对象数量不断增加,并且框架无法垃圾收集这些数据,因为它认为这些数据正在被使用。您需要查看代码并尝试找出泄漏的原因,内存分析器可能会有所帮助。
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, i.e. 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.
如果不依赖 GC,就无法将其从内存中删除。这将取决于 GC 何时以及如何释放内存。 GC 以不确定的方式工作,这意味着您无法确定 GC 何时真正释放实例。
您应该确保在不再需要 DataTable 时不再保留任何对 DataTable 的引用。如果你正确地完成了该部分,即除非需要,否则没有对 DataTable 的引用,那么你就可以了,剩下的就交给 GC 了
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 i.e. there is no reference to DataTable unless required then you are good and leave rest on GC
我怀疑尝试重建 GC 所做的事情是否是一个好的设计。
为什么不运行一个 24/7 运行的循环任务,将进程作为新线程启动?当线程完成后,您可以销毁它,这样就不会出现内存问题。
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.
需要垃圾收集来完全处理它,但如果您不再需要它,您可以随时将其设置为 null。
Garbage collection will be required to completely dispose of it, but if you no longer need it you can always set it to null.
您始终可以将数据表本身包装在 using 块中,而不是调用 dispose 方法:
这将确保当对象本身超出其范围时将其清除。
如果您从未使用过,MSDN 文章本身就是一个明显的资源以前也这样,但想法很简单。
Instead of calling the dispose method, you could always wrap the datatable itself in a using block:
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.