Beruflich Dokumente
Kultur Dokumente
6 important .NET concepts: - Stack, heap, Value types, reference types, boxing and Unboxing.
By Shivprasad koirala | 13 Feb 2012 | Unedited contribution
.NET1.0 .NET1.1 .NET2.0 SQL2000 SQL2005 VS.NET2003 VS2005 SQL-CE C#1.0 C#2.0
,+
6 important .NET concepts: - Stack, heap, Value types, reference types, boxing and Unboxing.
4.89 (172 votes)
6 important .NET concepts: - Stack, heap, Value types, reference types, boxing and Unboxing.
Introduction What goes inside when you declare a variable? Stack and Heap Value types and reference types So which data types are ref type and value type? Boxing and Unboxing Performance implication of Boxing and unboxing Source code
This video explains the concept of boxing and unboxing and it also shows the performance implications caused by the same.
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. 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 lot more click here , you can also catch me on my trainings @ click here.
1 de 6
21/03/2012 8:19
6 important .NET concepts: - Stack, heap, Value types, reference type... http://www.codeproject.com/Articles/76153/6-important-NET-conc...
It a 3 line code so let understand line by line how things execute internally. s s Line 1:- When this line is executed compiler allocates a small amount of memory in to memory type called as stack. Stack is responsible of keeping track of running memory needed in your application. Line 2:- Now the execution moves to the next step. As the name says stack it stacks this memory allocation on the top of the first memory allocation. You can think about stack as series of compartment or boxes put on top of each other. Memory allocation and de-allocation is 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 an 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 as Heap . Heap does not track running memory it just pile of objects which can s reached at any moment of time. Heap is used for dynamic memory allocation. One more important point to note here is reference pointers are allocated on 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 which are assigned on stack. In other words all variables which are related to data type are de-allocated int in 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 .
2 de 6
21/03/2012 8:19
6 important .NET concepts: - Stack, heap, Value types, reference type... http://www.codeproject.com/Articles/76153/6-important-NET-conc...
Now many of our developer friends must be wondering why two types of memory, canwe just allocate everything on just one t memory type and we are done. If you look closely primitive data types are not complex, they hold single values like i = 0Object data types are complex, they int . reference other objects or other primitive data types. In other words they hold reference to other multiple values and each one of them must be stored in memory. Object types need dynamic memory while primitive needs static type memory. If the requirement is of dynamic memory it allocated on a heap or else it goes on a stack. s
When we create an object and when we assign one object to the other object, they both point to the same memory location as show in the below code snippet. So when we assign to obj 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 .
3 de 6
21/03/2012 8:19
6 important .NET concepts: - Stack, heap, Value types, reference type... http://www.codeproject.com/Articles/76153/6-important-NET-conc...
4 de 6
21/03/2012 8:19
6 important .NET concepts: - Stack, heap, Value types, reference type... http://www.codeproject.com/Articles/76153/6-important-NET-conc...
If you compile the above code and see the same in ILDASM you can see in the IL code how boxing and unboxing looks, below figure demonstrates the same.
5 de 6
21/03/2012 8:19
6 important .NET concepts: - Stack, heap, Value types, reference type... http://www.codeproject.com/Articles/76153/6-important-NET-conc...
Currently I have not put a source code for unboxing but the same hold true for the same. You can write the 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
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
6 de 6
21/03/2012 8:19