GARBAGE COLLECTION FOR UNMANAGED CODE
The CLR cannot clean up database connections, window handles and file handles. So to release those resources developer has implement techniques.
For this explicit cleaning, the cleaning that is not handled by GC, we need to make use of .Net’s dispose() method. Though we can even make use of the Finalize() method. In C# it is generally used as the destructor, but it is under the control of the GC. So to clean up the unmanaged resources implement the IDisposable interface. It consists of the Dispose() method, and unlike Finalize is under developers control. A Dispose() method must contain a call to GC.SuppressFinalize() notifying the garbage collector that finalization is not needed on that object. It is recommend to use both Dispose() and Finalize() methods on the object which needs to clean up the unmanaged resource. The Finalize method will act as backup to the Dispose() , in case it is not called, thus preventing the leak of unmanaged resource.
WEAK REFERENCES:
A weak reference provides a mechanism for referencing an object with the advantage that it can be garbage collected . this concept is basically implemented by the ASP.Net caches. When the memory usage becomes too high the references are cleaned up. Can be implemented as the System.WeakReference class.
Forced Garbage Collection :
Garbage Collection can be forced by invoking GC.Collect() method of the System.GC class. In some cases it is advantageous to force the garbage collection to improve the performance. However, it should be used with at most care, since every time GC runs it suspends all current executing threads. The GC.Collect method should not be located in a place from where it can be called again and again, hence degrading the performance.
July 9, 2009
Garbage Collection Part2
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment