SIGN UP MEMBER LOGIN:    
ARTICLE

How memory is managed by Stack and Heap

Posted by Yogendra Kumar Articles | C# Language February 25, 2011
I studied many articles about memory management, but they are all confusing about when memory is allocated in the stack and when memory is allocated in the heap. But I always got confused. But finally, I got exactly what I wanted.
Reader Level:


How memory is managed in .Net?

I studied many articles about memory management, but they are all confusing about when memory is allocated in the stack and when memory is allocated in the heap. But I always got confused. But finally, I got exactly what I wanted.

Hopefully this article will help some of my friends who are still confused about the Stack and Heap.

In a simple way I can say that ....

As we know the Common Language Runtime (CLR) automatically handles Memory Management with the help of its components. Component one is the Garbage Collector which plays an important role in Memory Management. It contains two methods (Dispose and Finalize). Objects are automatically freed when they are no longer needed by the application through Garbage Collector.

When the Garbage Collector will be called to clean up unnecessary objects from memory. Non deterministic finalization is performed when the CLR foresees a memory deficiency.

How memory is managed for Value Type:

MemMgmt1.gif

In above figure you can see what happens in first two statements, simple value of variable a and b is storing in stack, now let's see what happen in 3rd and 4th statement.

MemMgmt2.gif

As we can see that simply changes the position where the variable is actually stored. So in these statements we can see that there is no use of Heap.

Now we will just see for Reference Type:

class Program
{
    public int value;

    static void Main(string[] args)
    {
        Program prog = new Program();
        prog.value = 100;
        Program prog1 = prog;
        prog1.value = 200;
    }
}

Now let's see how value will store in stack and heap in reference type.

Now we can see here for first statement...

MemMgmt3.gif

Now we will see for next two statements:

Program prog1 = prog;
prog1.value = 200;

MemMgmt4.gif
(Memory address for value 100) (Value)

Now we can see that the address is transferred from prog to prog1, and address is same so the value of that particular address is going to change as change is having on address so the value will be overriden in the Heap.

Hope you have gotten idea that how exactly reference type stores the value....

Now finally I will talk about very important thing that is how exactly string stores:

MemMgmt5.gif

class Program
{
    static void Main(string[] args)
    {
        string s = "Hello";
        string ss = s;

        s = s + " " + "and Welcome";
        Console.WriteLine(s);
        Console.WriteLine(ss);
        Console.ReadKey();
    }
}

Now we will see how value will store for this program:

For first statement:

string s = "Hello";

MemMgmt6.gif

For second statement...

string ss = s;

MemMgmt7.gif

In this diagram we can see that the address is transferred from s to ss, so as well as the value will also be transferred to ss, so in heap value will be overriden.
Now come for the next statement....

s = s + " " + "and Welcome";

MemMgmt8.gif

As in this image we can see that, as we just add "and welcome" in string "s", then the value is changed at the address but in the heap hello will be at it's own position so the value will not be overriden, and the new value of "s" will be stored at the new address which is here 6000. So now the address also will change for "s", now the new address for "s" is 6000.

Hope you understand that how exactly value stored in stack and heap...

Thanks...

By,
Yogendra Kumar

Login to add your contents and source code to this article
share this article :
post comment
 

stack and heap is little confusing with reference type.

Posted by Gaurav Gupta Dec 17, 2011

b,coz string is immutable data type .So, we can't edit the value of string type variable on the same reference,it always gives us a new reference. when we edit in string variable , it always gives us a new reference of memory on which we are edit. we have to hold this reference in string type variable to access the value.

Posted by Gaurav Gupta Dec 17, 2011

Nice article...Good to know about Stack and heap... Cheers .Net

Posted by Sachin Kalia Mar 08, 2011

Dear Ravi, I think you also din't focused on String type as i shown in figure. you just pass "Hello" to s, now in stack corresponding to s, one address like 2000 will be stored on the position of s in stack. and the value("Hello") will be stored in Heap. now you are changing the value of s, so simply the value will be override on same position of heap and on same address, so the value will be "Hello:World" on address 2000. Hope you would be understand that what exactly i wanna explain to u all.....

Posted by Yogendra Kumar Mar 01, 2011

what wil happen for this---- string s = "Hello"; s = s + ": " + "World";

Posted by Ravi K Kashyap Feb 25, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor