Wednesday, November 10, 2010

The .NET Framework - Garbage Collection

One of the most important benefits of using managed applications is the use of the garbage collector (GC). The role of CLR does not end after compiling the IL code into native code. In fact, the CLR is responsible for managing memory during the code execution. It assures that the memory used by the program is totally freed up after the program exits. With unmanaged applications, programmers are responsible for managing the memory and resolving problems that might occur if a block of memory is left allocated after the program ends.

With a managed application, blocks of memory are allocated on the managed heap. (The heap is the part of memory that is used to store objects, as opposed to the stack, which is used to store references to objects— more on heap and stack later.) The GC keeps track of the referenced objects on the heap and automatically frees up the memory allocated to a specific object when it goes out of scope. Calling the GC programmatically is possible by invoking the method System.GC.Collect. However, this is not recommended because it is not guaranteed that it will destroy your objects. It is best to focus on the business logic and let the CLR determine the right time for garbage collection.

Related Posts Plugin for WordPress, Blogger...

Popular Posts

Share |