Grisson's .net

源码之前,了无秘密

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

摘录
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.


   
    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.



关于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  海盗  阅读(344)  评论(0编辑  收藏  举报