C#如何立即回收内存

1.把对象赋值为null
2.立即调用GC.Collect();

注意:这个也只是强制垃圾回收器去回收,但具体什么时候执行不确定。 

代码:

    class Test
    {
        ~Test()
        {
            Console.WriteLine("DeConstructor.");
        }
 
    }
    class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test();
 
            t = null;
            GC.Collect();
 
            Console.Read();
        }
    }



posted @ 2013-07-04 09:10  muzizongheng  阅读(530)  评论(0编辑  收藏  举报
如果我们时时忙着展现自己的知识, 将何从忆起成长所需的无知?