Text Size

GARBAGE COLLECTION IN C#

PDF 

Tutorials

(2 votes, average: 5.00 out of 5)

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.

 

 

Trackback(0)
Comments (0)add comment

Write comment

busy

Site Statistics

Stats
Total Members
: 542
Total Discussion
: 0
Total Albums
: 0
Total Photos
: 0
Total Bulletins
: 0
Total Activities
: 1
Total Wall Posts
: 1