C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

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




Freeing Memory in Dot Net


Posted Date: 31 Jul 2006    Resource Type: Articles    Category: .NET Framework
Author: DotNetGuts (DNG)Member Level: Diamond    
Rating: Points: 10



.Net managed code enjoy's benefits of CLR, which automatically checks for object scope and if it is not referenced by any object than it is removed from memory.

But you can also manually frees memory....

Finalize() Method of Object class


Each class in C# is automatically (implicitly) inherited from the Object class which contains a method Finalize(). This method is guaranteed to be called when your object is garbage collected (removed from memory). You can override this method and put here code for freeing resources that you reserved when using the object.
For example

Protected override void Finalize()
{
try
{
Console.WriteLine(“Destructing Object….”);
//put some code here.
}
finally
{
base.Finalize();
}
}




Destructor


1) A destructor is just opposite to constructor.
2) It has same as the class name, but with prefix ~ (tilde).
3) They do not have return types, not even void and therefore they cannot return values.
4) destructor is invoked whenever an object is about to be garbage collected
Eg:

class person
{
//constructor
person()
{
}

//destructor
~person()
{
//put resource freeing code here.
}
}



What is the difference between the destructor and the Finalize() method? When does the Finalize() method get called?


Finalize() corresponds to the .Net Framework and is part of the System.Object class. Destructors are C#'s implementation of the Finalize() method. The functionality of both Finalize() and the destructor is the same, i.e., they contain code for freeing the resources when the object is about to be garbage collected. In C#, destructors are converted to the Finalize() method when the program is compiled. The Finalize() method is called by the .Net Runtime and we can not predict when it will be called. It is guaranteed to be called when there is no reference pointing to the object and the object is about to be garbage collected.

Garbage Collection


1) Garbage collection is the mechanism that reclaims the memory resources of an object when it is no longer referenced by a variable.
2) .Net Runtime performs automatically performs garbage collection, however you can force the garbage collection to run at a certain point in your code by calling System.GC.Collect().
3) Advantage of Garbage collection : It prevents programming error that could otherwise occur by incorrectly deleting or failing to delete objects.


-

DotNet Guts


"Lets Make Programming Easy"




Logon to DotNetGuts.2ya.com




Join us @ DotNetGuts@YahooGroups.com











Responses

Author: Sidhartha Gundavarapu    26 Aug 2006Member Level: Gold   Points : 0
Irrespective of the Finalize() or destructor... we can never control on when to free the memory.

So, the statement which says we can free the memory is not very correct. We can just deference... releasing memory is completely a functionality of GC and programmer cannot control that.

The only exception to my statement is if we are using unsafe code or using unmanaged code.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Constructor Concept in Dot Net
Previous Resource: Importing and Exporting data to Excel sheet
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use