C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Training   ASP.NET Web Hosting    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

Play Silverlight Games or Submit your Silverlight applications and earn 90% AdSense revenue.

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Forums » .NET » .NET »

Difference between dispose and gc.collect


Posted Date: 10 Aug 2008      Posted By: Supraja Chapram      Member Level: Silver     Points: 1   Responses: 6



Whether gc.collect() exist in .net? if so Difference between dispose and gc.collect()? what is the difference between finalize and dispose? What happenes when garbage collector runs?




Responses

Author: R.Sujaa    10 Aug 2008Member Level: SilverRating:     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 2008Member Level: SilverRating:     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 2008Member Level: GoldRating:     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 2008Member Level: SilverRating:     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 2008Member Level: DiamondRating:     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 2009Member Level: DiamondRating:     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



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : New Keyword
Previous : creating menu item
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use