Garbage collection

CLR will builds object graph, which represents each reachable object on the heap.
Generations 0 and 1 are termed ephemeral generations. As explained in the next section, you will see that the garbage collection

process does treat ephemeral generations differently.

.NET 4.0 changes the way the garbage collector deals with thread suspension when it cleans up objects on the managed heap, using

background garbage collection. Despite its name, this does not mean that all garbage collection now takes place on additional

background threads of execution.
System.GC
AddMemoryPressure()

RemoveMemoryPressure()
Allows you to specify a numerical value that represents the calling object's "urgency level" regarding the garbage collection process.

Be aware that these methods should alter pressure in tandem and thus never remove more pressure than the total amount you have

added.

Collect()
Forces the GC to perform a garbage collection. This method has been overloaded to specify a generation to collect, as well as the

mode of collection (via the GCCollectionMode enumeration).

CollectionCount()
Returns a numerical value representing how many times a given generation has been swept.

GetGeneration()
Returns the generation to which an object currently belongs.

GetTotalMemory()
Returns the estimated amount of memory (in bytes) currently allocated on the managed heap. A Boolean parameter specifies whether

the call should wait for garbage collection to occur before returning.

MaxGeneration
Returns the maximum number of generations supported on the target system. Under Microsoft's .NET 4.0, there are three possible

generations: 0, 1, and 2.

SuppressFinalize()
Sets a flag indicating that the specified object should not have its Finalize() method called.

WaitForPendingFinalizers()
Suspends the current thread until all finalizable objects have been finalized. This method is typically called directly after invoking

GC.Collect().
//coding on GC
long heapbytes = GC.GetTotalMemory(false);
int totalgenerations = GC.MaxGeneration + 1;
string mystring = "sample";
int mystringsGeneration = GC.GetGeneration(mystring);
// Force a garbage collection
GC.Collect();
GC.WaitForPendingFinalizers();
// Force a garbage collection on generation 0.
GC.Collect(0);
GC.WaitForPendingFinalizers();
object[] lotOfObjects = new object[50000];
for (int i = 0; i < 50000; i++)
lotOfObjects[i] = new object();

GC.Collect(0, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers();

// See if tonsOfObjects[9000] is still alive.
if (lotOfObjects[9000] != null)
{
int theirGeneration = GC.GetGeneration(lotOfObjects[9000]);
}
else
Console.WriteLine("lotOfObjects[9000] is no longer alive.(they are still live and in scope)");

// Print out how many times a generation has been swept.
int nTimesgeneration0Swept = GC.CollectionCount(0);
int nTimesgeneration1Swept = GC.CollectionCount(1);
int nTimesgeneration2Swept = GC.CollectionCount(2);
class MyResource :IDisposable
{
~MyResource()
{
// Clean up unmanaged resources here.
Console.Beep();
}
public void Dispose()
{
// Clean up unmanaged resources here.
}
}

MyResource rw = new MyResource();
if (rw is IDisposable)rw.Dispose();

MyResource rw2 = new MyResource();
try
{
// Use the members of rw.
}
finally
{
// Always call Dispose(), error or not.
rw2.Dispose();
}
using (MyResource rw3 = new MyResource())
{
// Use rw object.
}

using (MyResource rw4 = new MyResource(),rw5 = new MyResource())
{
// Use rw and rw2 objects.
}