Skip navigation

Garbage collection allows a small emulation of Class_Terminate event via the finalize method. However the finalize method does not supersede the authority of GC/CLR and it may not be instantly implemented if the GC/CLR assumes that the resource / object is still needed or in use.

 

It could very well be a couple of calls too late before it’s shut down.  This can kill you, especially when you need to remove the object for program flow.

 

  1. Finalized Objects are promoted to older generations causing unnecessary heap usage.
  2. Finalized Objects have longer initialization times

    .

  3. Finalized Objects are out of your control as to when and where they are actually terminated.
  4. Finalized objects cause any other objects that are associated with them to be finalized, adding more strain to the heap.
  5. Finalized objects can prolong the lifetime of other objects that are referenced from the finalized object

For the above reasons, it is better to avoid using Finalize by itself. If you decide to use it, make sure that all actions are avoided that could interfere with the finalize code, such as creating an instance of the finalized object after you run the finalize method, thread synchronization operations, and any exceptions from finalize method.

 

Resurrection is a side effect of finalization. Sometimes we’ll be presented with a situation in which an object has been finalized but there is still a pointer to it, meaning that Garbage Collection assumes it’s alive when it’s already been finalized.

 

A typical scenario is to finalize an object in order to create a new instance of the same object; if the first object is still there in finalization, the pointer points to the old object, and the object, while in finalized stage, never gets cleaned out properly because it’s got a reference from the application. It’s important to know that if you finalize something, you set a flag or a check routine to make sure that it’s gone before you try to do anything else concerning that object type.

 

Happy Learning

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish