Stack vs. Heap

What are the STACK and the HEAP?

To fully understand how memory is managed in the .NET Framework you need to understand the difference between the Stack and the Heap. This lesson introduces you to those differences and how it affects your development.

  • Variables that are directly managed on the Stack (or Call Stack) are called Value Types.
  • Variables that are managed on the Heap are called Reference Types.

How Do Variables On the Stack and Heap Differ?

The Stack (Value Types)

Value Types are data types that are directly managed on the stack. This means that when a variable is declared it directly contains its data on the stack.
Value Types include…

  • Integral types
    • sbyte
    • byte
    • short
    • ushort
    • int
    • uint
    • long
    • ulong
    • char
  • Floating Point types
    • float
    • double
  • Decimal type
    • decimal
  • Boolean type
    • bool
  • Enumerations
    • any enumeration created using “enum”
  • Structures
    • user defined datatype created using “struct”

The following code sample shows that Value Types directly contain their own data.

static void Main(string[] args)
{
int i; //this declaration allocates memory on the stack
    i = 5; //i is set to the value of 5

int j; //this declaration allocaties memory on the stack
    j = i; //j receives a copy of the value of i

j++; //increment j by one – since j has its own copy, i remains unaffected

//i = 5
//j = 6

}

The Heap (Reference Types)

Reference Types are managed on the heap. This means that when a variable is declared, the stack receives a reference to the memory on the heap. In other word, the variable does not directly contain its own data on the stack.
Reference Types include…

  • All objects that are created usng “class”

The following code sample show that Reference Types do not directly contain their own data.

static void Main(string[] args)
{
ArrayList list1; //this declaration allocates memory on the stack
    list1 = new ArrayList(); //new ArrayList() allocates memory on the heap and points to the reference
list1.Add(5);

ArrayList list2; span class=”comment”>//this declaration allocaties memory on the stack
list2 = list1; //list2 receives a copy of the value of list1 – however in this case the copy is only a copy of the reference
//both list1 and list2 are pointing to the same memory location on the heap

list2.Add(6); //add another item to list2 – because it is a reference type, both list1 and list2 are affected

//list1 contains 5 and 6
//list2 contains 5 and 6

}

So What?

So why is it important to understand the differences between how memory is managed on the stack and on the heap?
Memory that is allocated on the stack is released immediately when it is no longer referenced. However, memory that is allocated on the heap is not released immediately when it is no longer referenced. Garbage Collection is used to ensure that memory on the heap is released occasionally. See [Intro to Garbage Collection] for more information.



Categories: Misc

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: