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
I want high skilled C++ programmers Project Description: I want to hire the developer, who are proficient in C/C++ and have experience with STL and work with the data structu

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

IT governance defined In this section, a short literature survey will be conducted. The results will be presented in a final comparative table. Before starting defining IT gove

Problem: (a) (i) Using a diagram, illustrate the basic process in an ASP.NET Architecture involving at least five objects. (ii) List two methods and two collections

VB. Net & XML 1. Design a form based applications using labels, text boxes, and buttons to perform basic arithmetic operations on integers. 2. Explain the concept of Excepti

What's difference between "Optimistic" and "Pessimistic" locking? In the pessimistic locking when user wants to update a data it locks the record and till then no one can updat

Can you give an overview of ADO.NET architecture? The most important part  in the ADO.NET architecture is the "Data Provider". The  Data Provider provides access to the datasou

I want to develop SMS application with auto reply based on the content of the message using gsm modem. eg.Student results checking using cell phone via sms. How do i do this?

Name the classes that are introduced in the System.Numerics namespace. The following two new classes are introduced in the System.Numerics namespace:  BigInteger - Refers

I need C# Programmer Project Description: To help in the production of a C# module to provide a web service. We are based in Littlehampton, West Sussex. Skills required ar