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 is the difference between Stored Procedure (SP) and User Defined Function (UDF)? Some of major differences between a stored procedure and user defined are as follows:-

Differentiate between the while and for loop in C#. The while and for loops are used to implement those units of code that require to be repeatedly executed, unless the result

I need Online Slot Video Game For A Facebook Project Description: Prepare an online slot video game for a Face book promotion in order to attract new visits and Increase the

What are shared (VB.NET)/Static(C#) variables? Shared/Static classes are used when a class gives functionality which is not specific to any instance. In brief if you want an ob

TSQL Function SQL Server 2012 Project Description: I require someone to prepare a TSQL function that returns the name of the ODBC DSN. I will use the queries below, to get

how do I load txt file into listbox

What are the steps to create a webservice? This webservice will add 2 numbers and send  to the calling client.All the below steps shown are according to VS2005 beta editor :-

Explain different software development life cycles. The System Development Life Cycle (SDLC) is the overall process of developing information systems through multi stage proces

What is the difference between DELETE TABLE and TRUNCATE TABLE commands? Following are differences between delete & truncate commands:- 1)The DELETE TABLE syntax logs the de

How does output caching work in ASP.NET?    Output caching is a powerful method that enhances request/response throughput by caching the content generated from dynamic pages. O