When People Speak about Memeory management they speak about Garbage Collection.
And they start speaking about dispose,finalizer.Now let us see what all this hell is about and will try to explore the hell and get habituated to the hell.
Let me classical example ,now person A go to ATM to withdraw the money so as soon as he inserts the card , object will be created to him ,Now Person B comes to ATM to withdraw money even a object gets created as soon as he inserts the card
In the similar way some thousands of people visits ATM,so objects are created proportionally with the vistors.
So when objects get created it consumes memory ,now daily thousands of visitors and thousands of objects.So at some point of time I should free my memory to make ATM accessible quickly and effectively.
So there should be some regular cleanup of memory explicit or implicit.
Here in our DOTNET clr has got a weapon in its bag to deal with such scenarios ,it is none other than GARBAGE COLLECTOR.
Name itself suggests that it collects garbage,so all piled up objects which are free will be collected by GarbageCollector(GC) to free the memory.
Let us dive deep in to it.
When some object is found free with out any reference for longer duration,Garbage collector(GC) will push it to finalization queue ,next times when garbage collectoion is executed the objects in the que are given the generations means the the rating based on their lifeterm or age,so depending on that GC will call finalize method m on that object and destroy that.
Now the problem with this Finalize method is we don’t know when it is called and will be a implicit(internal) call by dotnet framework.
So what is option for me if I need to call the garbage collector explicitly as and when it is required.
Dispose is oasis in the desert ,this method can be called explicitly by our c# code to clean up objects.
There is interface called IDisposable(to know about t interface refer http://infobylakshmikanth.blogspot.com/2011/01/what-is-interface.html) which defines the 2 overloaded methods for implementing the dispose methods.
See the below code to implement IDisposable(code is taken from MSDN)
-----------------------------------------------------------------------------------------------------------
public class Base : IDisposable{
//Implement IDisposable.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Free other state (managed objects).
}
// Free your own state (unmanaged objects).
// Set large fields to null.
}
// Use C# destructor syntax for finalization code.
~Base()
{
// Simply call Dispose(false).
Dispose(false);
}
}
// Design pattern for a derived class.
public class Derived : Base
{
protected override void Dispose(bool disposing)
{
if (disposing)
{
// Release managed resources.
}
// Release unmanaged resources.
// Set large fields to null.
// Call Dispose on your base class.
base.Dispose(disposing);
}
// The derived class does not have a Finalize method
// or a Dispose method with parameters because it inherits
// them from the base class.
}
public class Derived : Base
{
protected override void Dispose(bool disposing)
{
if (disposing)
{
// Release managed resources.
}
// Release unmanaged resources.
// Set large fields to null.
// Call Dispose on your base class.
base.Dispose(disposing);
}
// The derived class does not have a Finalize method
// or a Dispose method with parameters because it inherits
// them from the base class.
}
Then in the next article we will discuss more about dispose and also avoid calling dispose method by “USING” keyword.
No comments:
Post a Comment