Garbage Collection completely absolves the developer from tracking memory usage and knowing when to free memory.Net GC is designed to collect "managed resources". Managed objects should not implement a Finalize method because of the below reasons:- - Finalizable objects increases memory pressure and prevents the object's memory from being collected when GC determines the object is garbage. - Finalizable objects take longer to allocate. - Finalizable objects may refer to other (non-finalizable) objects, prolonging their lifetime unnecessarily. - You have no control over when the Finalize method will execute. The object may hold on to resources until the next time the garbage collector runs. - The runtime doesn't make any guarantees as to the order in which Finalize methods are called.
However, there could be situations where in the developer may have to implement a Finalize method. The real reason being .Net GC is NOT designed to collect "unmanaged resources".GC doesn't know anything about the resource represented by the several other types type in memory.These resources could be file handles, sockets, kernel objects, network connection resource etc. The cleanup of these resources still lies in the hands of the developer.Finalizers are needed to guarantee the release of these resources back into the system. GC will only run if the system is not under memory pressure, unless it really needs to free up some memory. That means, you can never be sure when the GC will run.
Imagine you are a Database Connection. If you let the GC clean up after you, you may be connected to the database for much longer than needed, causing load. In that case, you want to implement Finalize. If situation arises that application must implement the Finalize method, then it should be made sure the code executes as quickly as possible.Also,avoid all actions that would block the Finalize method.
It is not recomended to write our own finalizer for cleanup.The objects that have their finalisers turn out to be expensive as it requires two cycles of garbage collection before they are actually cleaned up. The amount of time to clean such an object is doubled.Secondly, Garbage collection is non-deterministic in nature and there is no way to tell when the garage collection will happen.So a object with finalizer will hold on to the resources until the next cycle of Garbage collection happens. Since there is no guarentee of the order in which finalisers will be called it will impose more set of problem in case their is a sequential dependencies between object destructors.
Usually .net takes care of GC collection its own time intervals so, you don’t need to worry about the GC. But, you can force the GC for example : system.gc or object = null ; to come and get the objects and again it depends on the requirement like if the cache is filled and if it can’t accommodate space for your project etc.
This group is an initiative from "Vidya Vrat Agarwal" a .NET Passionate. This group is created with a vision to encourage and help people learn Microsoft .NET technology. This group also provides a platform to share ideas, thoughts and experiences an individual may have while learning and implementing .NET technologies.
3 comments:
Garbage Collection completely absolves the developer from tracking memory usage and knowing when to free memory.Net GC is designed to collect "managed resources".
Managed objects should not implement a Finalize method because of the below reasons:-
- Finalizable objects increases memory pressure and prevents the object's memory from being collected when GC determines the object is garbage.
- Finalizable objects take longer to allocate.
- Finalizable objects may refer to other (non-finalizable) objects, prolonging their lifetime unnecessarily.
- You have no control over when the Finalize method will execute. The object may hold on to resources until the next time the garbage collector runs.
- The runtime doesn't make any guarantees as to the order in which Finalize methods are called.
However, there could be situations where in the developer may have to implement a Finalize method.
The real reason being .Net GC is NOT designed to collect "unmanaged resources".GC doesn't know anything about the resource represented by the several other types type in memory.These resources could be file handles, sockets, kernel objects, network connection resource etc.
The cleanup of these resources still lies in the hands of the developer.Finalizers are needed to guarantee the release of these resources back into the system.
GC will only run if the system is not under memory pressure, unless it really needs to free up some memory. That means, you can never be sure when the GC will run.
Imagine you are a Database Connection. If you let the GC clean up after you, you may be connected to the database for much longer than needed, causing load. In that case, you want to implement Finalize.
If situation arises that application must implement the Finalize method, then it should be made sure the code executes as quickly as possible.Also,avoid all actions that would block the Finalize method.
It is not recomended to write our own finalizer for cleanup.The objects that have their finalisers turn out to be expensive as it requires two cycles of garbage collection before they are actually cleaned up. The amount of time to clean such an object is doubled.Secondly, Garbage collection is non-deterministic in nature and there is no way to tell when the garage collection will happen.So a object with finalizer will hold on to the resources until the next cycle of Garbage collection happens.
Since there is no guarentee of the order in which finalisers will be called it will impose more set of problem in case their is a sequential dependencies between object destructors.
Usually .net takes care of GC collection its own time intervals so, you don’t need to worry about the GC.
But, you can force the GC for example : system.gc or object = null ; to come and get the objects and again it depends on the requirement like if the cache is filled and if it can’t accommodate space for your project etc.
Post a Comment