




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、C# 中的垃圾回收机制很多系统都有其自身的垃圾回收,其回收机制大体是相同的。它们使程序员从跟踪内存使用的繁重任务中解脱出来。虽然大多数回收器都要求应用程序不时地暂停从而释放不再使用的内存。但C#中的回收器效率还是很高的。垃圾回收器的基本假定:1.被分配内存空间的对象最有可能被释放。在方法执行时,就需要为该方法的对象分配内存空间,搜索最近分配的对象集合有助于花费最少的代价来尽可能多地释放内存空间。2.生命期最长的对象释放的可能性最小,经过几轮垃圾回收后,对象仍然存在,搜索它时就需要进行大量的工作,却只能释放很小的一部分空间。3.同时被分配内存的对象通常是同时使用,将它们彼此相连有助于提高缓存性能
2、和回收效率 。C#中的回收器是分代的垃圾回收器(Gererational Garbage Collector) 它将分配的对象分为3个类别或代。(可用GC.GetGeneration方法返回任意作为参数的对象当前所处的代) 最近被分配内存的对象被放置于第0代,因为第0代很小,小到足以放进处理器的二级(L2)缓存,所以它能够提供对对象 的快速存取。经过一轮垃圾回收后,仍然保留在第0代中的对象被移进第1代中,再经过一轮垃圾内存回收后,仍然保留在第1代中的对象则被移进第2代中,第2代中包含了生存期较长的对象。在C#中值类型是在堆栈中分配内存,它们有自身的生命周期,所以不用对它们进行管理,会自动分配和
3、释放。而引用类型是在堆中分配内存的。所以它的分配和释放就需要像回收机制来管理。C#为一个对象分配内存时,托管堆可以立即返回新对象所需的内存,因为托管堆类似于简单的字节数组,有一个指向第一个可用内存空间的指针,指针像游标一样向后移动,一段段内存就分配给了正在运行的程序的对象。在不需要太多垃圾回收的程序小,托管堆性能优于传统的堆。当第0代中没有可以分配的有效内存时,就触发了第0代中的一轮垃圾回收,它将删除那些不再被引用的对象,并将当前正在使用的对象移至第1代。而当第0代垃圾回收后依然不能请求到充足的内存时,就启动第1代垃圾回收。如果对各代都进行了垃圾回收后仍没有可用的内存就会引发一个OutOfMe
4、moryException异常。终结器在有些情况下,类可以提供一个终结器在对象被销毁时执行,终结器是一个名为Finalize的受保护的方法:protected void Finalize()base.Finalize();/释放外部资源垃圾回收器使用名为“终止队列”的内部结构跟踪具有 Finalize 方法的对象。每次您的应用程序创建具有 Finalize 方法的对象时,垃圾回收器都在终止队列中放置一个指向该对象的项。托管堆中所有需要在垃圾回收器回收其内存之前调用它们的终止代码的对象都在终止队列中含有项。(实现 Finalize 方法或析构函数对性能可能会有负面影响,因此应避免不必要地使用它们
5、。用 Finalize 方法回收对象使用的内存需要至少两次垃圾回收。当垃圾回收器执行回收时,它只回收没有终结器的不可访问对象的内存。这时,它不能回收具有终结器的不可访问对象。它改为将这些对象的项从终止队列中移除并将它们放置在标为准备终止的对象列表中。该列表中的项指向托管堆中准备被调用其终止代码的对象。垃圾回收器为此列表中的对象调用 Finalize 方法,然后,将这些项从列表中移除。后来的垃圾回收将确定终止的对象确实是垃圾,因为标为准备终止对象的列表中的项不再指向它们。在后来的垃圾回收中,实际上回收了对象的内存。)Dispose方法在不使用终结器时,可以考虑使用Dispose方法,你可以使用这
6、个方法来释放所保存包括的在托管对象引用在内的任何资源。但使用它时需用GC.SuppressFinalize来告知运行时这些对象不需要终结。如下所示:public void Dispose()object.Dispose();dbConnection.Dispose();GC.SuppressFinalize(this); /申明不需要终结创建并使用了Dispose方法的对象,就需要使用完该对象之后调用这些方法,最好是在Finally中调用。/以下代码演示来自MSDN/ Design pattern for the base class./ By implementing IDisposable
7、, you are announcing that instances/ of this type allocate scarce resources.public class BaseResource : IDisposable/ Pointer to an external unmanaged resource.private IntPtr handle;/ Other managed resource this class uses.private Component Components;/ Track whether Dispose has been called.private b
8、ool disposed = false;/ Constructor for the BaseResource object.public BaseResource()/ Insert appropriate constructor code here./ Implement IDisposable./ Do not make this method virtual./ A derived class should not be able to override this method.public void Dispose()Dispose(true);/ Take yourself off
9、 the Finalization queue/ to prevent finalization code for this object/ from executing a second time.GC.SuppressFinalize(this);/ Dispose(bool disposing) executes in two distinct scenarios. / If disposing equals true, the method has been called directly/ or indirectly by a user's code. Managed and
10、 unmanaged resources / can be disposed./ If disposing equals false, the method has been called by the / runtime from inside the finalizer and you should not reference / other objects. Only unmanaged resources can be disposed. protected virtual void Dispose(bool disposing)/ Check to see if Dispose ha
11、s already been called.if (!this.disposed)/ If disposing equals true, dispose all managed/ and unmanaged resources.if (disposing)/ Dispose managed resources.Components.Dispose();/ Release unmanaged resources. If disposing is false,/ only the following code is executed.CloseHandle(handle);handle = Int
12、Ptr.Zero;/ Note that this is not thread safe./ Another thread could start disposing the object/ after the managed resources are disposed,/ but before the disposed flag is set to true./ If thread safety is necessary, it must be/ implemented by the client.disposed = true;/ Use C# destructor syntax for
13、 finalization code./ This destructor will run only if the Dispose method / does not get called./ It gives your base class the opportunity to finalize./ Do not provide destructors in types derived from this class. BaseResource()/ Do not re-create Dispose clean-up code here./ Calling Dispose(false) is
14、 optimal in terms of/ readability and maintainability.Dispose(false);/ Allow your Dispose method to be called multiple times, / but throw an exception if the object has been disposed. / Whenever you do something with this class,/ check to see if it has been disposed.public void DoSomething()if (this
15、.disposed)throw new ObjectDisposedException();/ Design pattern for a derived class./ Note that this derived class inherently implements the/ IDisposable interface because it is implemented in the base class. public class MyResourceWrapper : BaseResource/ A managed resource that you add in this deriv
16、ed class. private ManagedResource addedManaged;/ A native unmanaged resource that you add in this derived class. private NativeResource addedNative;private bool disposed = false;/ Constructor for this object.public MyResourceWrapper()/ Insert appropriate constructor code tected override void
17、 Dispose(bool disposing)if (!this.disposed)tryif (disposing)/ Release the managed resources you added in/ this derived class here.addedManaged.Dispose();/ Release the native unmanaged resources you added/ in this derived class here.CloseHandle(addedNative);this.disposed = true;finally/ Call Dispose
18、on your base class.base.Dispose(disposing);/ This derived class does not have a Finalize method/ or a Dispose method without parameters because it inherits/ them from the base class.System.GC类GC类包含了可使用户与垃圾回收机制进行互操作的静态方法,包括发起新一轮垃圾回收操作的方法。确定某对象当前所在代的方法及当前分配内存空间的方法。GC.Collect(); /无参时将发起一轮全面的回收。GC.Collect(i);/(0<=i<=2)对第i代进行垃圾回收。GetTotalMemory将返因分配于托管堆上的内存空间总量。当参数为True时,在计算之前将进行一轮全面的垃圾回收。如下所示:下面是 在.NET Framewo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 雇主保姆劳务合同范本
- 按日租车合同范本
- 赠与子女土地合同范本
- 重庆买卖合同范本
- 超市专柜扣点合同范本
- 中文版Creo-3.0基础教程-第4章-工程特征建模
- 正规私人借款合同范本
- 简单电脑租赁合同范本
- 承接镀锌加工合同范本
- 2025私人房屋交易合同样本
- 污水处理日常运营管理与维护方案
- 稀土磁性材料项目可行性研究报告申请备案
- 物业民法典知识培训课件
- 企业安全生产责任制管理制度模版(三篇)
- 中式面点知识培训课件
- 《水文监测单位安全生产标准化评价标准》
- 设备清洁管理制度内容
- 甘肃省2025届高三高考诊断(一诊)政治试卷(含答案解析)
- 品管圈PDCA案例-中医医院减少住院患者艾灸烫伤率医院改善成果汇报
- 供应商开发流程培训讲义
- 食品企业食品农药残留检测方案
评论
0/150
提交评论