Wednesday, May 7, 2008

Memory Managment (Stack and Heap)

It is extremely important to know how Memory works in C#. Firstly you need to know there are different types of memory in each programming language including C#. Two of those are Stack and Heap.

What is Stack?
Stack is kind of memory. It is like a box of memory. when application runs it will be filled to some extend with internal information then stack pointer shows next free space in the memory. Defining new variable means that free space will be allocated to that variable in stack and then stack pointer will point to next free space.

This is a sample of stack:

first it is like this
then let say you create an integer variable like this: (do not worry about syntax we will get back to that later on)

int a=10;

then the stack will change to this

let's say if you defined this variable inside a method if running process leave the method then a variable will be deleted from stack it means that stack pointer show like the first image again so it means you do not have access to a.

What is Heap?

Another type of memory but this time it is not in stack. Although this memory is not in stack, there is a pointer in stack that point to this memory. Let’s say heap is like a part of memory that we do not have access .As soon as we create a variable a reference to the address of heap will be allocated to a pointer from that time we can access to the heap with that pointer. To make it easier look at this picture:

Person p = new Person();
p.Name = "Peter";
p.Age = 10;

let's think we got a type name person ( do not care about syntax just think a complex type)

As you can see we have p which is in stack and we have an address in it which is the address of a variable in heap. What we have in stack will be called reference and what we have in heap right now we call it object. The reference is like remote control and the object is like TV. Sometimes programmers make mistake ant just define the stack like code below
Person P;
This code just makes the stack part. To assign a space in heap we have to create a new object like:
P = new Person();
By creating Person object we grantee that our remote control is connected to a TV!

Another point about the heap is that if we leave the scope of P then that object in heap will not be accessible at the same time this object will stay in memory and this is the place that Memory management come to play and delete all unnecessary heap memories.

which variables are stored in Heap and which ones are stored in Stack?

Good question! it depends on the variable type if the variable type is primitive like int double then the variable will be in stack otherwise it will be in heap. in further lessons all the c# types will be introduced and we will see each type is stored in which memory type


R.Harihararajan. said...

Your Beginner lesson use for me,
Because i am beginner of ASP.Net


balaji said...

Thank you for your posts. they are very useful.

balaji said...

thanks it seems basic but very useful

Anonymous said...

Thanks. Very clear.