Labels

Sunday, July 29, 2007

Object.Finalize


Below are the facts on Finalize() method.

 

·          Destructors are the C# mechanism for performing cleanup operations. Destructors provide appropriate safeguards, such as automatically calling the base type's destructor.

·          In C# code, Object.Finalize cannot be called explicitly or overridden. All you can do is implement a destructor. And note, when the C# compiler compiles a destructor, it implicitly translates the destructor code to the equivalent of a Finalize() method that ensures the Finalize() method of the parent class is executed.

·          This method is automatically called after an object becomes inaccessible, unless the object has been exempted from finalization by a call to SuppressFinalize.

·          Finalize is automatically called only once on a given instance, unless the object is re-registered using a mechanism such as ReRegisterForFinalize and GC.SuppressFinalize has not been subsequently called.

  • Every implementation of Finalize in a derived type must call its base type's implementation of Finalize.

 

  • A type must implement Finalize when it uses unmanaged resources such as file handles or database connections that must be released when the managed object that uses them is reclaimed.

 

 

·          Finalize operations have the following limitations:

·          The exact time when the finalizer executes during garbage collection is undefined.

·          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.

·          The thread on which the finalizer is run is unspecified.

·          Resources are not guaranteed to be released at any specific time, unless calling a Close method or a Dispose method.

 

·          The Finalize method might not run to completion or might not run at all in the following exceptional circumstances:

o         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.

o         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.

 

 

Regards

Arun.



Once upon a time there was 1 GB storage in your inbox. Click here for happy ending.



Get the freedom to save as many mails as you wish. Click here to know how.

No comments:

Post a Comment