.NET垃圾收集行为(与数据表OBJ) [英] .NET Garbage Collection behavior (with DataTable obj)

查看:95
本文介绍了.NET垃圾收集行为(与数据表OBJ)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很奇怪,为什么创建一个非常简单的数据表,然后将其设置为空后做垃圾收集不清除由数据表所使用的所有内存。下面是一个例子。之前,该变量应该等于删除,但事实并非如此。

  {
没过多久= 0,后= 0 ,移除= 0,累计= 0;

之前= GC.GetTotalMemory(真);
DataTable的DT = GetSomeDataTableFromSql();
=后,GC.GetTotalMemory(真);
DT = NULL;
=删除GC.GetTotalMemory(真);
GC.Collect的();
=收集GC.GetTotalMemory(真);
}



给出了以下结果。

 之前= 388116 
= 731248
移除= 530176
收集后= 530176


解决方案

GC.Collect的(); 仅仅是一个建议,垃圾收集器可能存在需要清理的对象。在GC运行在自己的计划,这是非常罕见的,它需要在 GC.Collect的(); 提示。



通过调用看到对记忆产生影响的几率 GC.Collect的(); 立即(微秒)后'已经发布了一个资源很渺茫



另外:DataTable对象不在GC的眼睛特别。在.NET任何引用类型将由以同样的方式对GC进行处理。


I am wondering why after creating a very simple DataTable and then setting it to null does the Garbage Collection not clear out all the memory used by that DataTable. Here is an example. The variable Before should be equal to Removed but it is not.

{
 long Before = 0, After = 0, Removed = 0, Collected = 0;

 Before = GC.GetTotalMemory(true);
 DataTable dt = GetSomeDataTableFromSql();
 After = GC.GetTotalMemory(true);
 dt = null;
 Removed = GC.GetTotalMemory(true);
 GC.Collect();
 Collected = GC.GetTotalMemory(true);
}

Gives the following results.

Before = 388116
After = 731248
Removed = 530176
Collected = 530176

解决方案

GC.Collect(); is merely a suggestion to the Garbage Collector that there may be objects that need to be cleaned up. The GC runs on its own schedule and it's very rare that it will need the GC.Collect(); prompting.

The chances of seeing an impact on memory by calling GC.Collect(); immediately (microseconds) after you've released a resource are slim.

Also: The DataTable object isn't special in the eyes of the GC. Any reference type in .NET will be treated by the GC in the same way.

这篇关于.NET垃圾收集行为(与数据表OBJ)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆