Why is it preferred to not use finalize for clean up
Why is it preferred to not use finalize for clean up?
Problem with finalize is that garbage collection has to make two rounds in order to remove
objects which have finalize methods.
Below figure will make things clear regarding the two rounds of garbage collection rounds performed for the objects having finalized methods.
In this scenario there are three objects Object1, Object2 and Object3. Object2 has the finalize method overridden and remaining objects do not have the finalize method overridden.
Now when garbage collector runs for the first time it searches for objects whose memory has to free. He can see three objects but only cleans the memory for Object1 and Object3. Object2 it pushes to the finalization queue.
Now garbage collector runs for the second time. He seeís 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 practice is not to write clean up Non.NET resources in Finalize method rather use the DISPOSE.
2007-10-23, 6193👍, 0💬
What is the difference between Class and structureís ? Following are the key differences between the...
What is cross page posting? By default, button controls in ASP.NET pages post back to the same page ...
.NET INTERVIEW QUESTIONS - How can you avoid deadlock in threading? A good and careful planning can ...
Can Sub Procedure/Function Be Called Recursively? - Oracle DBA FAQ - Creating Your Own PL/SQL Proced...
Regarding C Coding Given: Line in file Contents 30 int * someIDs, theFirst, *r; 110 someIDs =GetSome...