Saturday, December 3, 2011

Some do’s and Don’ts’s and tips on Finalize, Dispose and GC


1)      Override Finalize only when you want GC to perform cleanup for unmanaged resources.
2)      GC calls Finalize method just before reclaiming memory from that object, so you don’t have any control when Finalize method is called.
3)      Dispose method is called to cleanup managed resources and it can be called anytime when object is not needed in program by calling Dispose method.
4)      Objects which have destructor/Finalize methods take 2 steps before memory is claimed from them.
5)      If you don’t want Finalize to be called for a particular object, then call GC.SuppressFinalize for that object anytime, best place for this call would be in Dispose method. To re-register that object for Finalization you can call GC.ReRegisterForFinalize.
6)      Freachable is pronounced as F-Rechable and it means Finalizer-Reachable queue.
7)      Dispose method is not thread-safe, if your object is accessed by more than 1 thread then you should make Dispose a thread safe method.
8)      If your program uses a significant amount of unmanaged memory then you should call GC.AddMemoryPressure to make GC aware of memory chuck which was consumed by program through unmanaged way, and same way when memory is released for that unmanaged resource you can call GC.RemoveMemoryPressure .
9)      You can call GC.KeepAlive on an object when you fear that Object may be calimed by GC while its being used. This kind of scenario can occur when you have passed any object using ByRef to some unmanaged method , and that unmanaged application is using it forever.
10)   By default every application in 32 bit computer gets 2 GB of virtual address space.
11)   GC works in 2 modes which are Server and Workstation.
12)   Workstation garbage collection is always used on a computer that has only one processor, regardless of the <gcServer> setting. If you specify server garbage collection, the CLR uses workstation garbage collection with concurrency disabled.
13)   In case of Server GC mode, there is a separate dedicated thread which manages collection.
14)   Number of threads on server = Number of application * Number of processor.
15)   Since .Net 4 Non-concurrent garbage collection became Background garbage collection.
16)   Concurrent Garbage collection / Background Garbage collection is performed on Generation 2 items in Workstation mode.
17)   Background garbage collection is not currently available for server garbage collection.
18)   Background garbage collection can be defined as collection of generation 2 objects by a separated thread which is paused whenever there is foreground garbage collection happening.
19)   Concurrent garbage collection has a slightly bigger working set (compared with non-concurrent garbage collection), because you can allocate objects during concurrent collection. However, this can affect performance, because the objects that you allocate become part of your working set. Essentially, concurrent garbage collection trades some CPU and memory for shorter pauses.
20)   Background garbage collection removes allocation restrictions imposed by concurrent garbage collection, because ephemeral garbage collections can occur during background garbage collection.

21)   Finalizers should always be protected, not public or private so that the method cannot be called from the application's code directly and at the same time, it can make a call to the base.Finalize method.

22)  When an application instantiates a new object, if the object's type defines a Finalize method, a pointer to the object is placed on the finalization queue just before the type's instance constructor is called. The finalization queue is an internal data structure controlled by the garbage collector. Each entry in the list points to an object that should have its Finalize method called before the object's memory can be reclaimed.
23)  The garbage collector scans the finalization queue looking for pointers to the objects which are identified as garbage. And when found, it is moved to freachable queue which is another data structure maintained by garbage collector's internal. A special high-priority CLR thread is dedicated to calling Finalize methods and CLR uses a high priority thread to finalize these objects which appear in this freachable queue. The object in Freachable queue is reachable only to this finalization thread. So When writing the finalization method it should concentrate on disposing the local and native objects and shouldn't execute any that makes any assumptions about the thread that's executing the code.
24)   For more information on Garbage Collection please refer  http://msdn.microsoft.com/en-us/library/0xy59wtx.aspx.



No comments:

Post a Comment