Tutorials
Desktop Programming
C Sharp
GARBAGE COLLECTION IN C#
Written by Christopher Thursday, 29 May 2008 13:35
Tutorials
INTRODUCTION
A significant advantage of C# when compared to C++ is the memory management capabilities of the C#. The programmer need not worry about memory management; the garbage collector is assigned this operation on the programmer’s behalf. You will probably know that value data types are stored on the stack while reference data types are stored on the managed heap. The stack stores data value types that are not members of objects. Also, in C# it is always the case that if variable a goes into scope before variable b, then b will go out of scope first. For example, if you declare some variables in a method, these variables will go out of scope when the method ends. However, it maybe sometimes that you need to keep these variables long after the method/function ended. This happens for all data declared with the new operator, the reference types. All reference types are stored in the managed heap, which is under the control of garbage collector.
GARBAGE COLLECTION BASICS
When you declare a reference type instance, a pointer of that instance is created on the stack, and the actual object in the managed heap. When assigning the value of one reference type to another, another variable is created on the stack. When a variable that points to an object goes out of scope it is removed from the stack, but the data in the managed heap remain until the program terminates or the garbage collector removes them. When the garbage collector runs it will remove all unreferenced objects from the managed heap. Then, the managed heap will contain scattered data that form the objects still referenced to. After the removal of the useless data, the garbage collector moves the remaining objects to new positions in order to create a single, contiguous block of data. Then the pointers for these objects are updated to their new values. Garbage collector is in charge of all actions mentioned above. Garbage collector runs when the .Net environment determines that it is required to run. However, you can force the garbage collector to run by calling the GC.Collect() method. There are no guarantees though, that an unreferenced object will be disposed in the first run of the garbage collector. Objects that do not have a destructor or finalizer get removed in a single pass, whereas objects with destructors need two passes to be disposed. The first pass calls the destructor and the second one deletes the object. Garbage collector is not able to clear unmanaged resources such as file handles, database and network connections and others. When your program references unmanaged resources you must take care to ensure that these resources are released when the garbage collector runs. This can be accomplished by the declaration of a destructor for the object oy by implementing the System.IDisposable interface in it.

| < Prev | Next > |
|---|
| Training Tip #1 :: Push-ups on the Swiss Ball admin 28.4.2009 7:07 |
Re:hello admin 21.4.2008 14:11 |
| Dell Inspiron 1545 Windows XP Drivers admin 12.4.2009 8:03 |
hello yuppy 21.4.2008 10:28 |
| Re:Get rich ebooks - are they really working? lann 23.4.2008 17:04 |
Re:Get rich ebooks - are they really working? admin 19.4.2008 14:08 |