Saturday, 28 February 2009

Memory managment in c#

Garbage Collection is the backbone of memory managment . In short, GC Consists of the following steps:

  1. The GC searches for managed objects that are referenced in managed code.
  2. The GC attempts to finalize objects that are not referenced.
  3. The GC frees objects that are not refernced and reclaim memory from them.

This is a fantastic blog on using finalize and and dispose method in C#

http://www.devx.com/dotnet/Article/33167/0/page/1.

Stack Vs Heap

Memory Type Stack Heap
Contents Value Types Objects
Stack Frames

Item Order LIFO Random
LifeTime Scope Reference Counter
Item Removal POP Garbage Collection
Timing Deterministic Non Deterministic

Please refer the following post for this http://en.csharp-online.net/Stack_vs._Heap

C# Variable : Value type and Reference Type

In any programming language , Variables hold a very important place. While working with variables , we should keep in mind




  1. Variables are declared as being of particular type.

  2. Each variable is constrained to hold only valuesof its declared type

In C# , there are two types of variable : Value Type and Reference Type


Value Type:


All value types are derived implicitly from the System.ValueType. It includes all predefined datatypes , Structs and Enumerators.


In the following code , two variables are declared and set with the integer values.


int x = 1;


int y = x;


y=2;


after this statement , x holds the value 1 and y holds the value 2.

Reference Type:

The predefined reference types are objects and string. User defined types are:

  • Class
  • Interface
  • Delegate

Reference types actually hold the value of a memory address occupied by the object they reference .Consider the following piece of code , in which two variables are given a reference to the same object.

Object x = new Object();

x.newValue = 1

Object y = x

y.newValue = 2

After this statement both x.newValue and y.newValue equal to 2.

Strings , though they are reference type , they work more like value types.

String s1 = "Hello";

String s2 = s1;

s2 = "Bye";

After this statement s1 holds "Hello" and s2 holds "Bye".

Its because immutable property of the String and when the value of the s1 change , a new string object is created .

One should declare a type as a value type if all the following are true

  • The type acts like a primitive type
  • The type does not need to inherit from anyother type
  • The type will not have any othe types derived from it.
  • Objects of the type are not frequently passed as method arguments since this would cause frequent memory copy operation hurting performance.



Learn C# : Boxing and unboxing

I was struggling to understand the concept of boxing and unboxing. ThenI came across a fantastic blog of Jeffrey Richter.

http://msdn.microsoft.com/en-gb/magazine/cc301569.aspx

It cleard my concept on exactly how boxing and unboxing works. All the examples are very good and helped me to ubderstand this better