6 Important .NET Concepts - Stack, Heap, Value Types, Reference Types, Boxing and Unboxing

Embed Size (px)

Citation preview

  • 8/9/2019 6 Important .NET Concepts - Stack, Heap, Value Types, Reference Types, Boxing and Unboxing.

    1/10

    6 important .NET concepts: - Stack, heap,Value types, reference types, boxing andUnboxing.

    ByShivprasad koirala

    6 important .NET concepts: - Stack, heap, Value

    types, reference types, boxing and Unboxing.

    C# (C#1.0, C#2.0, C#3.0, C#4.0), .NET (.NET1.0,.NET1.1, .NET2.0, .NET3.0, .NET3.5, .NET4.0),ASP.NET, SQL-Server (SQL2000, SQL2005, SQL-CE,SQL2008), Visual-Studio (VS.NET2003, VS2005,VS2008, VS2010), Architect

    Revision: 2 (See All)

    Posted: 27 Apr 2010Updated: 4 May 2010

    Views: 12,677

    Bookmarked: 106 times

    Unedited contribution

    76 votes for this article.

    Popularity: 8.51 Rating: 4.52 out of 5

    1 2 3 4

    56 important .NET concepts: - Stack, heap, Value types, reference types, boxing and Unboxing.IntroductionWhat goes inside when you declare a variable?Stack and HeapValue types and reference typesSo which data types are ref type and value type?Boxing and UnboxingPerformance implication of Boxing and unboxingSource code

    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 thenit moves ahead to explain 2 important concepts stack and heap. Article then talks about reference typesand 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 unboxingwith a sample code.

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

    http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=1335831http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=1335831http://www.codeproject.com/script/Articles/ListVersions.aspx?aid=76153http://www.codeproject.com/script/Articles/Unedited.aspxhttp://www.codeproject.com/script/Articles/TopArticles.aspx?ta_so=1http://www.codeproject.com/KB/dotnet/6importentStepsDotNet.aspx?display=Print#Introductionhttp://www.codeproject.com/KB/dotnet/6importentStepsDotNet.aspx?display=Print#What%20goes%20inside%20when%20you%20declare%20a%20variablehttp://www.codeproject.com/KB/dotnet/6importentStepsDotNet.aspx?display=Print#Stack%20and%20Heaphttp://www.codeproject.com/KB/dotnet/6importentStepsDotNet.aspx?display=Print#Value%20types%20and%20reference%20typeshttp://www.codeproject.com/KB/dotnet/6importentStepsDotNet.aspx?display=Print#So%20which%20data%20types%20are%20ref%20type%20and%20value%20typehttp://www.codeproject.com/KB/dotnet/6importentStepsDotNet.aspx?display=Print#Boxing%20and%20Unboxinghttp://www.codeproject.com/KB/dotnet/6importentStepsDotNet.aspx?display=Print#Performance%20implication%20of%20Boxing%20and%20unboxinghttp://www.codeproject.com/KB/dotnet/6importentStepsDotNet.aspx?display=Print#Source%20codehttp://tinyurl.com/mra3hxhttp://tinyurl.com/y4mbsn6http://www.codeproject.com/script/Articles/ListVersions.aspx?aid=76153http://www.codeproject.com/script/Articles/Unedited.aspxhttp://www.codeproject.com/script/Articles/TopArticles.aspx?ta_so=1http://www.codeproject.com/KB/dotnet/6importentStepsDotNet.aspx?display=Print#Introductionhttp://www.codeproject.com/KB/dotnet/6importentStepsDotNet.aspx?display=Print#What%20goes%20inside%20when%20you%20declare%20a%20variablehttp://www.codeproject.com/KB/dotnet/6importentStepsDotNet.aspx?display=Print#Stack%20and%20Heaphttp://www.codeproject.com/KB/dotnet/6importentStepsDotNet.aspx?display=Print#Value%20types%20and%20reference%20typeshttp://www.codeproject.com/KB/dotnet/6importentStepsDotNet.aspx?display=Print#So%20which%20data%20types%20are%20ref%20type%20and%20value%20typehttp://www.codeproject.com/KB/dotnet/6importentStepsDotNet.aspx?display=Print#Boxing%20and%20Unboxinghttp://www.codeproject.com/KB/dotnet/6importentStepsDotNet.aspx?display=Print#Performance%20implication%20of%20Boxing%20and%20unboxinghttp://www.codeproject.com/KB/dotnet/6importentStepsDotNet.aspx?display=Print#Source%20codehttp://tinyurl.com/mra3hxhttp://tinyurl.com/y4mbsn6http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=1335831
  • 8/9/2019 6 Important .NET Concepts - Stack, Heap, Value Types, Reference Types, Boxing and Unboxing.

    2/10

    Image taken fromhttp://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 in to the RAM. This

    memory has 3 things first the name of the variable, second data type of the variable and finally the valueof the variable.

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

    in more details.

    http://michaelbungartz.wordpress.com/http://michaelbungartz.wordpress.com/http://michaelbungartz.wordpress.com/
  • 8/9/2019 6 Important .NET Concepts - Stack, Heap, Value Types, Reference Types, Boxing and Unboxing.

    3/10

  • 8/9/2019 6 Important .NET Concepts - Stack, Heap, Value Types, Reference Types, Boxing and Unboxing.

    4/10

    Now many of our developer friends must be wondering why two types of memory, cant we just allocateeverything on just one memory type and we are done.

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

    Image taken fromhttp://michaelbungartz.wordpress.com/

    Value types and reference types

    http://michaelbungartz.wordpress.com/http://michaelbungartz.wordpress.com/http://michaelbungartz.wordpress.com/
  • 8/9/2019 6 Important .NET Concepts - Stack, Heap, Value Types, Reference Types, Boxing and Unboxing.

    5/10

    Now that we have understood the concept of Stack and Heap its time to understand the concept ofvalue types and reference types.

    Value types are types which hold both data and the memory on the same location. While a reference typehas a pointer which points to the memory location.

    Below is a simple integer data type with name i whose value is assigned to an other integer data typewith 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 complete different copy. In other word ifyou change either of them the other does not change. These kinds of data types are called as Valuetypes.

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

    In other words if we change one of them the other object is also affected this is termed as Referencetypes.

  • 8/9/2019 6 Important .NET Concepts - Stack, Heap, Value Types, Reference Types, Boxing and Unboxing.

    6/10

    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 andObjects are reference types and any other .NET primitive data types are assigned on the stack. Below

  • 8/9/2019 6 Important .NET Concepts - Stack, Heap, Value Types, Reference Types, Boxing and Unboxing.

    7/10

    figure explains the same in a more detail manner.

    Boxing and Unboxing

    WOW, you have given so much knowledge, so whats the use of it in actual programming. One of thebiggest implications is to understand the performance hit which is incurred due to data moving from stackto heap and vice versa.

    Consider the below code snippet. When we move a value type to reference type the data is moved fromthe stack to the heap. When we move reference type to a value type the data is moved from the heap tothe stack.

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

    When the data moves from value types to reference types its termed as Boxing and the vice versa is

  • 8/9/2019 6 Important .NET Concepts - Stack, Heap, Value Types, Reference Types, Boxing and Unboxing.

    8/10

    termed as UnBoxing.

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

  • 8/9/2019 6 Important .NET Concepts - Stack, Heap, Value Types, Reference Types, Boxing and Unboxing.

    9/10

    Performance implication of Boxing and unboxing

    In order to see how the performance is impacted we ran the below two functions 10,000 times. Onefunction has boxing and the other function is simple. We used a stop watch object to monitor the time

    taken.

    The boxing function was executed in 3542 MS while without boxing the code was executed in 2477 MS. Inother words try to avoid boxing and unboxing. In project you always need boxing and unboxing , use itwhen its absolutely necessary.

  • 8/9/2019 6 Important .NET Concepts - Stack, Heap, Value Types, Reference Types, Boxing and Unboxing.

    10/10

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

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

    Source code

    Attached with article is a simple code which demonstrates how boxing creates performance implications.You can download the source code from here

    http://www.codeproject.com/KB/dotnet/6importentStepsDotNet/Source_Code.ziphttp://www.codeproject.com/KB/dotnet/6importentStepsDotNet/Source_Code.zip