6 Important .NET Concepts

Introduction

This article will explain 6 important concepts Stack, heap, value types, reference types, boxing, and unboxing. This article starts first explaining what happens internally when you declare a variable and then it moves ahead to explain 2 important concepts stack and heap. The article then talks about reference types and value types and clarifies some of the important fundamentals around them.

Finally, the article concludes by demonstrating how performance is hampered due to boxing and unboxing with a sample code.

Watch my 500 videos on various topics like design patterns, WCF, WWF, WPF, LINQ, Silverlight, UML, Sharepoint, Azure, VSTS, and a lot more click here, you can also catch me on my training @ click here.

WCF
The image is taken from http://michaelbungartz.wordpress.com/

What goes inside when you declare a variable? When you declare a variable in a .Net application, it allocates some chunk of memory into the RAM. This memory has 3 things first the name of the variable, second data type of the variable, and finally the value of the variable.

That was a simple explanation of what happens in the memory, but depending on what kind of data type your variable is allocated on that type of memory. There are two types of memory allocation stack memory and heap memory. In the coming sections, we will try to understand these two types of memory in more detail.

Memory

Stack and Heap

In order to understand stack and heap, let's understand what actually happens in the below code internally.

public void Method1() {  
    // Line 1  
    int i = 4;  
    // Line 2  
    int y = 2;  
    // Line 3  
    class1 cls1 = new class1();  
} 

It's a 3 line code so let's understand line by line how things execute internally.

Line 1

When this line is executed compiler allocates a small amount of memory into a memory type called the stack. Stack is responsible for keeping track of the running memory needed in your application.

Line 2

Now the execution moves to the next step. As the name says it stacks this memory allocation on top of the first memory allocation. You can think about a stack as a series of compartments or boxes put on top of each other.

Memory allocation and de-allocation are done using LIFO (Last in first out) logic. In other words, memory is allocated and de-allocated at only one end of the memory i.e. top of the stack.

Line 3

In line 3 we have a created object. When this line is executed it creates a pointer on the stack and the actual object is stored in a different type of memory location called 'Heap'. 'Heap' does not track running memory it's just a pile of objects which can reach at any moment in time. Heap is used for dynamic memory allocation.

One more important point to note here is reference pointers are allocated on the stack. The statement, Class1 cls1; does not allocate memory for an instance of Class1, it only allocates a stack variable cls1 (and sets it to null). The time it hits the new keyword it allocates on "HEAP".

Exiting the method (The fun):- Now finally the execution control starts exiting the method. When it passes the end control it clears all the memory variables that are assigned on the stack. In other words, all variables that are related to the 'int' data type are de-allocated in the 'LIFO' fashion from the stack.

The BIG catch - It did not de-allocate the heap memory. This memory will be later de-allocated by "GARBAGE COLLECTOR".

GARBAGE COLLECTOR

Now many of our developer friends must be wondering why two types of memory, can't we just allocate everything on just one memory type and we are done?

If you look closely at primitive data types that are not complex, they hold single values like 'int i = 0'. Object data types are complex, they reference other objects or other primitive data types. In other words, they hold a reference to other multiple values and each one of them must be stored in memory. Object types need dynamic memory while primitive need static type memory. If the requirement is of dynamic memory it's allocated on a heap or else it goes on a stack.

Static memory
The image is taken from http://michaelbungartz.wordpress.com/

Value types and reference types Now that we have understood the concept of 'Stack' and 'Heap' it's time to understand the concept of value types and reference types.

Value types are types that hold both data and memory in the same location. While a reference type has a pointer that points to the memory location.

Below is a simple integer data type with the name 'i' whose value is assigned to another integer data type with the name 'j'. Both these memory values are allocated on the stack.

When we assign the 'int' value to the other 'int' value it creates a completely different copy. In other words, if you change either of them the other does not change. These kinds of data types are called 'Value types'.

Public void method

When we create an object and when we assign one object to the other object, they both point to the same memory location as shown in the below code snippet. So when we assign 'obj' to 'obj1' they both point to the same memory location.

In other words, if we change one of them the other object is also affected this is termed as 'Reference types'.

Reference type

So which data types are ref type and value type?

In .NET depending on data types the variable is either assigned on the stack or on the heap. 'String' and 'Objects' are reference types and any other .NET primitive data types are assigned on the stack. the Below figure explains the same in a more detailed manner.

Value type

Boxing and Unboxing

WOW, you have given so much knowledge, so what's the use of it in actual programming? One of the biggest implications is to understand the performance hit that is incurred due to data moving from stack to heap and vice versa. Consider the below code snippet. When we move a value type to a reference type the data is moved from the stack to the heap. When we move the reference type to a value type the data is moved from the heap to the stack.

This movement of data from the heap to the stack and vice-versa creates a performance hit.

When the data moves from value types to reference types it's termed as 'Boxing' and vice versa is termed as 'UnBoxing'.

Unboxing

If you compile the above code and see the same in ILDASM you can see in the IL code how 'boxing' and 'unboxing' look, the below figure demonstrates the same.

ILDASM

Performance implication of boxing and unboxing

In order to see how the performance is impacted, we ran the below two functions 10,000 times. One function has boxing and the other function is simple. We used a stopwatch object to monitor the time taken. The boxing function was executed in 3542 MS while without boxing the code was executed in 2477 MS. In other words, try to avoid boxing and unboxing. In a project you always need boxing and unboxing, use it when it's absolutely necessary.

With the same article, the sample code is attached which demonstrates this performance implication.

Implication

Currently, I have not put a source code for unboxing but the same holds true for the same. You can write the same and experiment with it by using the stopwatch class.

Source code Attached to the article is a simple code that demonstrates how boxing creates performance implications. You can download the source code from the top of this article.