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
Explain MVC and how it is diffrennt from Asp.net

ASP.NET/MSSQL/ MVC completing backend solution + integrating with frontend. Project Description: System is used for tracking company's activities through several projects by

How can we load multiple tables in a DataSet? objCommand.CommandText = "Table1" objDataAdapter.Fill(objDataSet, "Table1") objCommand.CommandText = "Table2" o

Passport authentication A centralized service given by Microsoft, offers a single logon point for clients. Unauthenticated users are redirected to the Passport site

List the new features added in .NET Framework 4.0. The following are the latest features of .NET Framework 4.0:     Improved Application Compatibility and Deployment Support

Differentiate between managed and unmanaged code? Managed code is the code that is implemented directly by the CLR instead of the operating system. The code compiler first comp

Question: (a) Define four special target names use with the TARGET attribute. (b) Assume that you have a document with many subsections. Write the HTML code to create a fr

Want Microsoft CRM Dynamics CRM 2011 or 2013 programmer Project Description: Need to do customizations in CRM itself and integrate CRM 2011 with SAP Business One and HTML web

1  Assignment Description   This is an individual assignment. Students are referred to the Faculty's policy on plagiarism.  In this assignment you will build a prototype of a si

Print Pyramids of Numbers - C# Program Hello can you please provide me some examples regarding to the Print Pyramids of Numbers in C#.