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
Project Description: I require a C# Console project written to simply enter an Origin and Destination/Date on the United Airlines Site, from the returned page select and return

What is a node? A network can have of two or more computers directly linked by some physical medium such as coaxial cable or optical fiber. Such a physical medium is known as L

What is difference between data set and data reader? Some major differences between dataset and datareader are as folows :- 1) The DataReader gives forward-only and read-onl

What are the authentication methods in .NET? 1.Windows authentication 2.Forms authentication 3.Passport authentication 4.None/custom authentication

I need .net expert help for Advance Project First let me start by saying that I have been writing code for almost 25 years. Therefore I am only looking for someone to aid advanc

What is RAID and how does it work? The Redundant Array of Independent Disks (RAID) is a term used for describing the technique of improving data availability through the use of

What is AppSetting Section in "Web.Config" file? Web.config file describe the configuration for a webproject. By Using "AppSetting" section we can define user defined values. T

Develop a Face Recognition Systems Project Description: Seeking for someone who has worked with Face Recognition techniques and is familiar with the relevant mathematical alg

I need Kinect Fitting Room - Smart Fitting Mirror I'm presently looking for someone able to create software exactly like this fitnect (Kinect Fitting Room). Software can work

Bug Correction and Prediction Project Description: The project is about the prediction of bugs and resolves the bug in the sample program using costriage algorithm. You have