| Author: R.Sujaa 10 Aug 2008 | Member Level: Silver | Rating: Points: 6 |
differences
Finalize:
.NET Garbage collector does almost all clean up activity for your objects. But unmanaged resources (ex: - Windows API created objects, File, Database connection objects, COM objects etc) is outside the scope of .NET framework we have to explicitly clean our resources. For these types of objects .NET framework provides Object.Finalize method which can be overridden and clean up code for unmanaged resources can be put in this
Dispose:
This is the best way we do clean our unallocated resources and yes not to forget we do not get the hit of running the Garbage collector twice.
Collect:
System.GC.Collect() forces garbage collector to run.This is not recommended but can be used if situations arises.
|
| Author: R.Sujaa 10 Aug 2008 | Member Level: Silver | Rating: Points: 5 |
The .NET object does this:
GC.Collect(GC.MaxGeneration);
GC.WaitForPendingFinalizers();
GC.Collect(GC.MaxGeneration);
With the 1.1 framework, the above approach was sufficient to cause any RCWs created for a .NET client that were no longer referenced explicitly in the .NET client code to be collected. That in turn allowed the RCWs to release my COM objects.
Now with 2.0, this no longer seems to work reliably.
|
| Author: chandramohan 11 Aug 2008 | Member Level: Gold | Rating: Points: 0 |
Finalize:
.NET Garbage collector does almost all clean up activity for your objects. But unmanaged resources (ex: - Windows API created objects, File, Database connection objects, COM objects etc) is outside the scope of .NET framework we have to explicitly clean our resources. For these types of objects .NET framework provides Object.Finalize method which can be overridden and clean up code for unmanaged resources can be put in this
Dispose:
This is the best way we do clean our unallocated resources and yes not to forget we do not get the hit of running the Garbage collector twice.
Collect:
System.GC.Collect() forces garbage collector to run.This is not recommended but can be used if situations arises.
|
| Author: Sareesh.V 11 Aug 2008 | Member Level: Silver | Rating: Points: 3 |
Finalize:
.NET Garbage collector does almost all clean up activity for your objects. But unmanaged resources (ex: - Windows API created objects, File, Database connection objects, COM objects etc) is outside the scope of .NET framework we have to explicitly clean our resources. For these types of objects .NET framework provides Object.Finalize method which can be overridden and clean up code for unmanaged resources can be put in this
Dispose:
This is the best way we do clean our unallocated resources and yes not to forget we do not get the hit of running the Garbage collector twice.
Collect:
System.GC.Collect() forces garbage collector to run.This is not recommended but can be used if situations arises.
|
| Author: Bunty 21 Sep 2008 | Member Level: Diamond | Rating: Points: 6 |
Hi,
Following the difference between Dispose and Finalize method,
1>CLR uses the Dispose and Finalize methods for performing garbage collection of runtime objects of .Net applications.
2>Clr has a Garbage Collector(GC) which periodically checks for unused and unreferenced objects in Heap.It call Finalize() method to free the memory used by such objects.
3>Dispose is another method which is invoked by Garbage Collector to release the memory occupied by an object.Dispose method needs to be explicitly called in code for removing an object from Heap.
4>Dispose method can be invoked only by the classes that IDisposable interface.
Regards S.S.Bajoria
Thanks & Regards S.S.Bajoria
|
| Author: Sridhar R 23 Feb 2009 | Member Level: Diamond | Rating: Points: 4 |
Finalize : 1.Finalize() is called by the runtime 2.Is a destructor, called by Garbage Collector when the object goes out of scope. 3.Implement it when you have unmanaged resources in your code, and want to make sure that these resources are freed when the Garbage collection happens.
Dispose : 1.Dispose() is called by the user 2.Same purpose as finalize, to free unmanaged resources. However, implement this when you are writing a custom class, that will be used by other users. 3.Overriding Dispose() provides a way for the user code to free the unmanaged objects in your custom class.
Verify these links for more ideas.. http://kyapoocha.com/aspnet-interview-questions/what-is-the-difference-between-finalize-and-dispose/ http://www.allinterview.com/showanswers/28556.html
Regards Sridhar R Nothing is illegal, Until You Get Caught
|