When the garbage collector performs a collection, it checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory.
If we manage memory ourselves we face mostly two issue:
1) Memory Leak :- If you allocate some memory and forget to release it , it is memory leak.
2) Dangling Pointer :- If you assign a memory location to two pointers and free to one, than second pointer is said dangling pointer since it is pointing to memory that is already released.
Which object is eligible for Garbage Collection:
int iTotal = 0;
CSum objCSum = new CSum();
int iTotal = C.FirstNumber + C.SecondNumber;
Console.WriteLine(“Total is “ + iTotal);
When control comes to line “int iTotal = 0; than there is no way to access object objCSum created in Sum function. So this object is eligible for Garbage Collection, You can say if an object goes out of scope than this object is eligible for garbage collection.
How Garbage Collector Works :- When we run a program a bunch of memory allocated to that program. All memory required for objects of this program allocated from this bunch.This bunch of memory divided in three parts which are called Generation0, Generation1 and Generation2. respectively.
Generation0 has smaller size, Generation1 has medium size and Generation2 has large size.
When we create a object using new it searches space in first part if it is find allocated else garbage collector to run for generation0 to get the space for new object.If it is an heavy object means need more bytes than generation 0 has than it takes the memory in generation1.
Garbage collection is a two step process.
1) Mark :- In first step Garbage collection mark the object which are eligilble for Garbage collection.
2) Compact :- In second step garbage collection run and free the memory occupied by marked object.
If garbage collector free the object from middle than there will be blank memory in the middle. But memory should be compact all freed space should be in last. Garbage collector do one task here shift all remaining object to generation1. And now generation 0 is again empty for new object.
If all generations are filled than MemoryOutofRange exception will be thrown..
Grabage Collector class and methods:- System.GC class represent the Garbage Collector.
Collect(..) :- This method is used to force Garbage Colelcttion for all generation or for a specific.
public static void Colelct()
public static void Colelct(int iGeneration);
GetTotalMemory(boolean IsWait) :- Returns the total number of bytes allocated in managed heap. If IsWait is true it waits for garbage collector to finish.
KeepAlive(object obj):- Extend the life time of an object passed to it as a parameter.
SuppressFinalize:- Suppress the finalize method if there is no need of it.
GetGeneraion(object obj):- Returns the current generation of an object.
ReRegisterRorFinalize :- Reregister an object for finalization. Means make it eligible for finalization.
WaitForPendingFinalizers() :- This method block the current thread till the execuation of all the pending finalizers over.
Destructor :- If we have opened some file or a database connection that all the work of closing the file or closing the connection get done in destructor. In C++ distructor called when we free memory. In C++ we release memory explicitly (by calling free objCSum). But in dot net we don’t do it explicitly it is garbage collection responsibility to free it and no one know it when it will do it.So there is no concept of destructor in dot net. When we write a desturctor in C#, C# compiler internally replace it by Finalize method. In Vb.Net we have Finalize method in place of Destructor in C#.
Dispose :- All the work that we do in destructor we do here in Dispose method. We have to call this method explicitly, dot net runtime does not know about this method.There is a IDiposable interface in dot net which contain only one method Dispose.We can implement this interface if we want Dispose method strictly.
Name does not matter you can write any custom method name ClearRes(). But there is a benefit to implement IDisposable and use Dispose method instead of any other method when we use using keyword it automatically call Dispose method for that object.
CSum objCSum = new CSum();
} // here it will Dispose method
//This is equivalent to try finally. If any error occurs between using block, finally always called. And inside finally object destructor will be called.
To ensure that Dispose called at least once you can call it in Finalize method.But there is no guarantee that GC will call Finalize method on all objects which implements methods.If you have called Dispose method explicitly and written a calling in Finalize too then you can suppress the Finalize method by calling