zoukankan      html  css  js  c++  java
  • C#系统之垃圾回收

    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();
  • 相关阅读:
    正则表达式
    网络编程
    多线程
    IO—Data
    IO-对象流
    IO-转换流
    异常
    常用类——File——Random——Math——枚举
    每月博客-20180310
    每月博客-20180204
  • 原文地址:https://www.cnblogs.com/zhangtaotqy/p/8971483.html
Copyright © 2011-2022 走看看