zoukankan      html  css  js  c++  java
  • .NET垃圾回收笔记

      名词

        垃圾收集目标

    •       ephemeral GC

            发生在Gen 0 和Gen 1 的垃圾收集

    •       Full GC

            发生Gen 2 及以上的Gen与LOH的垃圾收集

        垃圾收集模式

    •       工作站模式

            GC直接发生在内存分配的线程(也是当前的工作托管线程)上

    •       服务器模式

            每个CPU核都有一个自己独立的GC线程与托管堆

        垃圾收集的并发需求

    •       并发的意思是background GC在进行垃圾收集时,其它托管线程是否可以同时工作(包括分配内存)
    •       仅需为Full GC考虑并发,因为ephemeral GC的时间短、代价小,可一直为阻塞模式。
    •       .NET4后的并发可让background GC与ephemeral GC同时工作

      配置

        

    <configuration>
        <runtime>
            <gcConcurrent enabled="true"/>
            <gcServer enabled="true"/>
        </runtime>
    </configuration>

       继承与垃圾回收模型

    using System;
    
    
    namespace 继承与垃圾回收模型 {
        class Base : IDisposable {
            private bool _disposed;
    
            protected virtual void Dispose(bool disposing) {
                Console.WriteLine(string.Format("Base.Dispose({0})", disposing));
                if (!_disposed) {
                    if (disposing) {
                        #region 清理Base自己的托管资源
                        #endregion
                    }
                    #region 清理Base自己的非托管资源
                    #endregion
                }
                _disposed = true;
            }
    
            public void Base干活() {
                if (_disposed) {
                    throw new ObjectDisposedException("Base");
                }
            }
    
            ~Base() {
                Console.WriteLine("~Base()");
                Dispose(false);
            }
    
            public void Dispose() {
                Console.WriteLine("");
                Dispose(true);
                GC.SuppressFinalize(this);
            }
        }
    
    
        class Child : Base {
            private bool _disposed;
    
            protected override void Dispose(bool disposing) {
                Console.WriteLine(string.Format("Child.Dispose({0})", disposing));
                if (!_disposed) {
                    try {
                        if (disposing) {
                            #region 清理Child自己的托管资源
                            #endregion
                        }
                        #region 清理Child自己的非托管资源
                        #endregion
                        _disposed = true;
                    }
                    finally {
                        base.Dispose(disposing);
                    }
                }
            }
    
            public void Child干活() {
                if (_disposed) {
                    throw new ObjectDisposedException("爸爸");
                }
            }
    
            //“可以”为Child写析构方法,但没必要。内部的“Dispose(false)”也没必要
            ~Child() {
                Console.WriteLine("~Child()");
            }
        }
    
        class 回收示例 {
            /// <summary>
            /// 输出:
            /// -----以下为手动回收
            /// Child.Dispose(True)
            /// Base.Dispose(True)
            ///
            /// -----以下为自动回收
            /// ~Child()
            /// ~Base()
            /// Child.Dispose(False)
            /// Base.Dispose(False)
            /// </summary>
            /// <param name="args"></param>
            static void Main(string[] args) {
                Console.WriteLine("-----以下为手动回收");
                var 手动 = new Child();
                手动.Dispose();
    
                Console.WriteLine("/n/n-----以下为自动回收");
                var 自动 = new Child();
                GC.Collect();
            }
        }
    }
    

      

      资料

        实现 Dispose 方法

        垃圾回收通知

        .Net Discovery 系列之七--深入理解.Net垃圾收集机制(拾贝篇) 发布在新年第一秒

        .Net Discovery 系列之四--深入理解.Net垃圾收集机制(下)

        So, what’s new in the CLR 4.0 GC?      
        .NET 4/4.5里新的垃圾收集机制      
        对象代(Generation)与GC      
        Using GC Efficiently – Part 2      
        CLR探索系列:Server and Workstation Garbage Collection探索(垃圾回收系列)
          

  • 相关阅读:
    20200721_34代码如何优化以及框架的运用和构建设计
    20200719_33appium应用及H5页面元素定位下
    20200709_29多线程与分布式
    day4_day4_python
    python_day3
    python第二天
    pytihon初学第一天
    移动平均线系列
    网页爬取
    十种经典排序算法
  • 原文地址:https://www.cnblogs.com/beta2013/p/3377259.html
Copyright © 2011-2022 走看看