Hi, Q. Finalize methods should be avoided. Should I implement Finalize on my class? Finalize method allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection The following is an extract from MDSN " Finalize operations have the following limitations: 1. The exact time when the finalizer executes during garbage collection is undefined. Resources are not guaranteed to be released at any specific time, unless calling a Close method or a Dispose method.
2. The finalizers of two objects are not guaranteed to run in any specific order, even if one object refers to the other. That is, if Object A has a reference to Object B and both have finalizers, Object B might have already finalized when the finalizer of Object A starts.
3. The thread on which the finalizer is run is unspecified. And also the Finalize method might not run to completion or might not run at all in the following exceptional circumstances: 1. Another finalizer blocks indefinitely (goes into an infinite loop, tries to obtain a lock it can never obtain and so on). Because the runtime attempts to run finalizers to completion, other finalizers might not be called if a finalizer blocks indefinitely.
2. The process terminates without giving the runtime a chance to clean up. In this case, the runtime's first notification of process termination is a DLL_PROCESS_DETACH notification.
3. The runtime continues to Finalize objects during shutdown only while the number of finalizable objects continues to decrease.
4. If Finalize or an override of Finalize throws an exception, the runtime ignores the exception, terminates that Finalize method, and continues the finalization process. And also Object.Finalize does nothing by default. It must be overridden by a derived class only if necessary, because reclamation during garbage collection tends to take much longer if a Finalize operation must be run.
Finalize can take any action, including resurrecting an object (that is, making the object accessible again) after it has been cleaned up during garbage collection. However, the object can only be resurrected once; Finalize cannot be called on resurrected objects during garbage collection. "
Q. Does using a destructor is good in net? does it interfere with GC working?
Coming from a C++ background, destructors look like the first, last and the only hope. But .NET provides other options like the IDispose method for this purpose, which MS recomends. It might interfere with GC working based upon way the objects are getting called. |