GC.Collect强制对所有“代进行”垃圾回收 GC.ReRegisterForFinalize请求系统调用指定对象的完成器方法 GC.SuppressFinalize()与上面相反,请求系统 不 调用指定对象的完成器方法 示例:using System;namespace ReRegisterForFinalizeExample { class MyMainClass { static void Main() { // Create a MyFinalizeObject. MyFinalizeObject mfo = new MyFinalizeObject(); // Release the reference to mfo. mfo = null; // Force a garbage collection. GC.Collect(); // At this point mfo will have gone through the first Finalize. // There should now be a reference to mfo in the static // MyFinalizeObject.currentInstance field. Setting this value // to null and forcing another garbage collection will now // cause the object to Finalize permanently. MyFinalizeObject.currentInstance = null; GC.Collect(); } } class MyFinalizeObject { public static MyFinalizeObject currentInstance = null; private bool hasFinalized = false; ~MyFinalizeObject() { if(hasFinalized == false) { Console.WriteLine("First finalization");
// Put this object back into a root by creating // a reference to it. MyFinalizeObject.currentInstance = this;
// Indicate that this instance has finalized once. hasFinalized = true; // Place a reference to this object back in the // finalization queue. GC.ReRegisterForFinalize(this); } else { Console.WriteLine("Second finalization"); } } } }
GC.ReRegisterForFinalize请求系统调用指定对象的完成器方法
GC.SuppressFinalize()与上面相反,请求系统 不 调用指定对象的完成器方法
示例:using System;namespace ReRegisterForFinalizeExample
{
class MyMainClass
{
static void Main()
{
// Create a MyFinalizeObject.
MyFinalizeObject mfo = new MyFinalizeObject(); // Release the reference to mfo.
mfo = null; // Force a garbage collection.
GC.Collect(); // At this point mfo will have gone through the first Finalize.
// There should now be a reference to mfo in the static
// MyFinalizeObject.currentInstance field. Setting this value
// to null and forcing another garbage collection will now
// cause the object to Finalize permanently.
MyFinalizeObject.currentInstance = null;
GC.Collect();
}
} class MyFinalizeObject
{
public static MyFinalizeObject currentInstance = null;
private bool hasFinalized = false; ~MyFinalizeObject()
{
if(hasFinalized == false)
{
Console.WriteLine("First finalization");
// Put this object back into a root by creating
// a reference to it.
MyFinalizeObject.currentInstance = this;
// Indicate that this instance has finalized once.
hasFinalized = true; // Place a reference to this object back in the
// finalization queue.
GC.ReRegisterForFinalize(this);
}
else
{
Console.WriteLine("Second finalization");
}
}
}
}
指向这个table,然后调用这个类时,先给这个table附值,然后用类=null;
可是这个时候table也被回收了,
class void myclass ():IDisposable
{
private static DataTable mytable=null;
Puble DataTable Table{set{mytable=value;}get{return mytable;}}
public void Dispose()
{
GC.SuppressFinalize(Table);
}
}
//调用
using (myclass mc=new myclass)
{
mc.Table=...//附值;
System.Console.WriteLine(myTable.rows.count); //显示为100
}
myclass amc=new myclass
amc=null;
GC.collect();
System.Console.WriteLine(myTable.rows.count); //错误,我想既然说不回收了,应该有显示啊
小弟生性驽钝,请兄明示