RSS

GARBAGE COLLECTION IN.NET FRAMEWORK

10 Jun

Garbage collection is a process of releasing the memory used by the objects, which are no longer referenced. This is done in  different ways and different manners in various platforms and languages. From the very beginning, we have used destructors, or deleted the allocated memory while using the other programming languages like C or C++. .NET on the other hand forbids the programmer to bother about it .We will see how garbage collection is being done in .NET.

BASIS OF GARBAGE COLLECTION

Resources such as Databases, File System Objects, etc. are being used by almost every programs that are written by us. Firstly a chunk of memory is allocated using the “new”operator. Then the object is initialized using the constructor. After that, we access its members by using the dot operator(.). Atlast we clear the memory.

When we look at the above steps, its looks very easy. But most of us forget to release the meory block  while going through the above steps. C++ has got a special member function called Destructor. It has the same name of the constructor or the class with the ‘~’ (tilde) symbol to start with. This is a special kind of function which will be called every time by the system when ever the system finds that object will not be used any more by the program.

But most of the times programmers forgot to release the memory. This is really a worth noticing problem which may lead us to memory leak.

GARBAGE COLLECTION IN .NET

The above stated problem led to the rise of Garbage Collector in .NET where the programmer does not have to bother about the memory deallocation and the framework does it automatically. Garbage collector is responsible for destroying objects for you.

Whenever a program is loaded into the memory, a chunk of memory is allocated for that particular program to store and manage objects. This memory is called Managed Heap. This memory is categorized into three generations:

  • Generation 0 : This is the youngest generation and contains short-lived objects. An example of a short-lived       object is a temporary variable. Garbage collection occurs most frequently in this generation. Newly allocated objects mostly comes in this generation.

  • Generation 1: This generation contains short-lived objects and serves as a buffer between short-lived objects and long-lived objects.
  • Generation 2: This generation contains long-lived objects. An example of a long-lived object is an object in a server application that contains static data that is live for the duration of the process.HOW GARBAGE COLLECTOR WORKS???

    Garbage Collector is implemented as a separate thread. It runs at the back end and has the lowest priority. As soon as the system finds that there is no space in the managed heap, the garbage collector thread gets the realtime priority. When the garbage collector thread runs, all other thread running in your application will temporarily halt. The steps that the garbage collector takes are as follows:

    1. It builds a map of all reachable objects keeping in mind that circular references do not make trouble. Any object not in this map is termed as unreachable.

    2. It checks whether any of the unreachable objects has a destructor that needs to be run and places them in the freachable queue.

    3. It deallocates the remaining unreachable objects by moving the reachable objects down the heap and thus updating the references at the same time.

    4. At this point, other threads are resumd.

    5. It finalizes the unreachable objects by its own thread.

    HOW TO INVOKE GARBAGE COLLECTION MANUALLY


    We know that the Garbage Collector automatically runs in the background and frees the memory. But we can invoke the garbage collector in the program by calling the GC.Collect().However calling the above function is not recommended. The above method starts the garbage collector , but the process runs asynchronously, and when the method call is complete, you still don’t know whether your objects have been destroyed or not.

    Some changes are there in the signature of GC.Collect() in .NET 4.5 framework. Initially, GC.Collect only allowed you to use Optimization call. Now it has 4 overloads:

    1. GC.Collect() : Forces an immediate garbage collection of all generations.

    2. GC. Collect(Int32) : Forces an immediate garbage collection from generation 0 through a specified generation.

    3. GC.Collect(Int32, GCCollectionMode): Forces a garbage collection from generation 0 through a specified generation, at a time specified by a GCCollectionMode value.

    4. GC.Collect(Int32, GCCollectionMode, Boolean): Forces a garbage collection from generation 0 through a specified generation, at a time specified by a GCCollectionMode value, with a value specifying whether the collection should be blocking.



About these ads
 
Leave a comment

Posted by on June 10, 2012 in .NET BASICS

 

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: