Effective C#--Chapter2

本文介绍了垃圾回收器(GC)对托管内存的控制,指出开发者需自行清理非托管资源。阐述了对象终结器对GC的影响,因终结器与GC不同线程,会使对象在内存停留更久、代升高。还说明了.NET GC的代机制,以优化工作,不同代对象检查频率不同。

摘录
The Garbage Collector(GC) controls managed memory for you.But the GC is not magic.You need to clean up after yourself,too.You are response for unmanaged resource such as file handles,database connections,GDI+ objects,COM objects, and other system objects.

Figure 2.1. The Garbage Collector not only removes unused memory, but it moves other objects in memory to compact used memory and maximize free space.

f2.1.JPG
   
    The finalizer for an object is called at some time after it becomes garbage and before the system reclaims its memory.This nondeterministic finalization means that you cannot control the relationship between when you stop using an object and when its finalizer executes.
 
   Relying on finalizers also intruduces performance penalties.Object that require finalizetion put a performance drag on the Garbage Collector.When the GC finds that an object is garbage but also requires finalization,it cannot remove that item from memory just yet.First it calls the finalizer.Finalizers are not executes bu the same thread that collects Garbage.Instread,the GC places each object that is ready for finalization in a queue and spawns(产生) yet another thread to execute all the finalizers.It continues with its business,removing other garbage from memory.On the next GC cycle,those object that have been finalized are removed from momory.

由于finalizer于GC不是同一线程,导致在一个对象请求finalize时GC不能马上从内存中清楚他,要起动另一个线程来执行finalize,至少要等到下一次GC执行时才能清楚他,这回导致这个对象的generation升高,更加不容易清楚了

Figure 2.2. This sequence shows the effect of finalizers on the Garbage Collector. Objects stay in memory longer, and an extra thread needs to be spawned to run the Garbage Collector.

f2.2.JPG

关于generation
   The .net Garbage Collector defines generations to optimize its work.Generations help the GC identitfy the likeliest garbage candidates more quickly.
   Any Object created since the last garbage collection operation is a generation 0 object. Any object that has survived one GC operation is a generation 1 object. Any object that has survived two or more GC operation is a generation 2 object.
   The purpose of generations is to separate local variables and objects that stay around for the life of the application.
Generation 0 object are mostly local variables.Member variable and global variables quickly enter generation1 and eventually generation 2.
   The GC optimizes its work by limiting how often it examines first- and second-generation objects. Every GC cycle examines generation 0 objects. Roughly 1 GC out of 10 examines the generation 0 and 1 objects. Roughly 1 GC cycle out of 100 examines all objects。Think about finalization and its cost again: An object that requires finalization might stay in memory for nine GC cycles more than it would if it did not require finalization. If it still has not been finalized, it moves to generation 2. In generation 2, an object lives for an extra 100 GC cycles until the next generation 2 collection.(??不是很理解)

posted on 2005-08-12 11:36 海盗 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Grisson/archive/2005/08/12/213266.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值