Why is it preferred to not use finalize for clean up, DOT NET Programming

The Problem with finalize is that the garbage collection has to make 2  rounds in order to remove the  objects which have finalize methods.

The figure below will make things clear regarding the two rounds of garbage collection rounds performed for the objects having finalized methods.

In this scenario there are 3 objects Object1, Object2 and Object3. The Object2 has the finalize method overridden and remaining objects do not have the finalize method overridden.

Later  when garbage collector runs for the first time it searches for the objects whose memory has to free. He can see 3 objects but it only cleans the memory for Object1 and Object3.The Object2 it pushes to the finalization queue.

Now garbage collector runs for the second time. He views that  there are no objects to be released and then checks for the finalization queue and at this moment it clears object2 from the memory.

So if you notice that object2 was released from memory in the second round and not first. That's why the best method is not to write clean up Non.NET resources in Finalize method rather use the DISPOSE.

 

2474_Finalize.png

Posted Date: 9/22/2012 3:22:06 AM | Location : United States







Related Discussions:- Why is it preferred to not use finalize for clean up, Assignment Help, Ask Question on Why is it preferred to not use finalize for clean up, Get Answer, Expert's Help, Why is it preferred to not use finalize for clean up Discussions

Write discussion on Why is it preferred to not use finalize for clean up
Your posts are moderated
Related Questions
What are the different types of workflow in WF? In WF 4.0, the following two types of workflows are used: Flowchart workflows - Helps you to make workflows using the commo

What is Post Cache substitution? The Post cache substitution is used when we want to cache the complete page but also require some of the dynamic region inside that cached page

Site minder Authentication in SharePoint We have fit site minder agent in SharePoint 2010. We would like to have incorporate with site minder agent. Currently site-minder age

Wowza expert required Project Description: Seeking wowza expert to teach our webmaster. To provide: How configure wowza to live transcoder. How allow add live transc

Converting temperature from Fahrenheit to Celsius - Program C# I am facing some problems in the code for Converting temperature from Fahrenheit to Celsius. Can anybody suggest

In this task, you are required to create an Account Management application with Web Form. Your Account Management application must have the following pages: 1.  Create an accoun

Require an expert Web Developer Our small business wants an experienced web developer to provide ongoing technical support for two websites. Experience with ASP.NET is a must.

What are the advantages of Distributed Processing? Benefits of distributed programming are:- a. Security/Encapsulation b. Faster Problem solving c. Distributed databas

Give a brief introduction to Binders. Binders are used by DLR to communicate with not the .NET Framework but also with a variety of other services, like Silverlight and COM. Th

Describe the role that XSL can play while dynamically generating HTML pages from a relational database. The SQLXML 3.0 and advanced versions give the facility of mapping the SQ