1. 程式人生 > >C#系統之垃圾回收

C#系統之垃圾回收

namespace opened closed weak reference aps sed public src

1.

技術分享圖片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            person p1 = new person();
            p1.Name 
= "ZhangTao"; WeakReference wkr = new WeakReference(p1); p1 = null; GC.Collect(); // 強制進行垃圾回收 //object wP1 = wkr.Target; //if (wP1 != null) //{ // Console.WriteLine(((person)wP1).Name);
//} //else //{ // Console.WriteLine("對象已被回收"); //} ThreadPool.QueueUserWorkItem(h => { object wP1 = wkr.Target; if (wP1 != null) { Console.WriteLine(((person)wP1).Name); }
else { Console.WriteLine("對象已被回收"); } }); Console.ReadKey(); } } class person { public string Name { get; set; } } }
GC.Collect();

C#系統之垃圾回收