天天看點

Effective C#--Chapter2

Effective C#--Chapter2

摘錄:

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.
Effective C#--Chapter2

    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.

Effective C#--Chapter2

關于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