Managed Vs UnManaged Resources:
CLR Garbage Collector for Managed Resources:
The Resourses which CLR handles directly are called managed ones and these items will be disposed after usage by the CLR. But in .net we can also use resources like File streams, windows handles, database connections, COM objects( generally the resource which wraps OS types)...which are actually handled by win32 itself and can be termed as Unmanaged resources which we need to handle explicitley in code.
CLR Garbage Collector for Managed Resources:
Actually CLR Garbage collector will free the managed resources memory implicitely. Here GC will examins the managed heap and all the non referenced objects in managed heap will be destroyed and free up the memory. For the unmanaged objects, GC dont have any idea and we need to explicitly ask GC to clean up the same. For this we impliment the Object.Finalize() method and will write code for releasing unmanaged objects . GC will call the object.Finalize() method during clean up operation and will perform the operation mentioned in the finalize method. In C# Finalize() methods are nothing but destrucotrs. So Finalize() will automatically call the destructor of the class.
The garbage collection GC class provides the GC.Collect method, which you can use to give your application some direct control over the garbage collector. In general, you should avoid calling any of the collect methods and allow the garbage collector to run independently.
Limitation of Finalize() => Implementing Finalize methods or destructors can have a negative impact on performance and you should avoid using them unnecessarily.The garbage collector keeps track of objects that have Finalize methods, using an internal structure called the finalization queue. Each time your application creates an object that has a Finalize method, the garbage collector places an entry in the finalization queue that points to that object. The finalization queue contains entries for all the objects in the managed heap that need to have their finalization code called before the garbage collector can reclaim their memory. So Reclaiming the memory used by objects with Finalize methods requires at least two garbage collections.
When the garbage collector performs a collection,
step1: It reclaims the memory for inaccessible objects without finalizers. At this time, it cannot collect the inaccessible objects that do have finalizers. Instead, it removes the entries for these objects from the finalization queue and places them in a list of objects marked as ready for finalization. Entries in this list point to the objects in the managed heap that are ready to have their finalization code called. The garbage collector calls the Finalize methods for the objects in this list and then removes the entries from the list.
step2 : A future garbage collection will determine that the finalized objects are truly garbage because they are no longer pointed to by entries in the list of objects marked as ready for finalization. In this future garbage collection, the objects' memory is actually reclaimed.
If We dont want to wait for GC and Need to free the memory (Recommended):
Programers can use patterns like disposable patterns to explicitly free up the managed/unmanaged resources without waiting for the GC . Please find my post The Disposable Pattern for more details.
The garbage collection GC class provides the GC.Collect method, which you can use to give your application some direct control over the garbage collector. In general, you should avoid calling any of the collect methods and allow the garbage collector to run independently.
Limitation of Finalize() => Implementing Finalize methods or destructors can have a negative impact on performance and you should avoid using them unnecessarily.The garbage collector keeps track of objects that have Finalize methods, using an internal structure called the finalization queue. Each time your application creates an object that has a Finalize method, the garbage collector places an entry in the finalization queue that points to that object. The finalization queue contains entries for all the objects in the managed heap that need to have their finalization code called before the garbage collector can reclaim their memory. So Reclaiming the memory used by objects with Finalize methods requires at least two garbage collections.
When the garbage collector performs a collection,
step1: It reclaims the memory for inaccessible objects without finalizers. At this time, it cannot collect the inaccessible objects that do have finalizers. Instead, it removes the entries for these objects from the finalization queue and places them in a list of objects marked as ready for finalization. Entries in this list point to the objects in the managed heap that are ready to have their finalization code called. The garbage collector calls the Finalize methods for the objects in this list and then removes the entries from the list.
step2 : A future garbage collection will determine that the finalized objects are truly garbage because they are no longer pointed to by entries in the list of objects marked as ready for finalization. In this future garbage collection, the objects' memory is actually reclaimed.
If We dont want to wait for GC and Need to free the memory (Recommended):
Programers can use patterns like disposable patterns to explicitly free up the managed/unmanaged resources without waiting for the GC . Please find my post The Disposable Pattern for more details.
No comments:
Post a Comment