6 Important one. NET concepts: stacks, heaps, value types, reference types, boxing, unpacking

Source: Internet
Author: User

Introduction

This article mainly introduces. NET 6 Important concepts: stacks, heaps, value types, reference types, boxing, unpacking. This article begins with a description of what happens inside the compiler when you declare a variable, and then introduces two important concepts: stack and heap, and finally introduces value types and reference types, and explains some important principles about them.

Finally, a simple example code is introduced to illustrate the performance loss of the packing unboxing.

Internal mechanism for declaring variables

In a. NET program, when you declare a variable, a chunk of memory is allocated in memory. This memory is divided into three parts: 1, variable name, 2, variable type, 3, variable value.

reveals the internal mechanism for declaring a variable, where the type of memory allocated depends on your variable type: There are two types of memory in net: Stack memory and heap memory. In the next section, we'll learn more about both types of content.

  

Stacks and heaps

To understand what stacks and heaps are, let's look at the internal mechanics of the following sample code:

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


Here's a total of 3 lines of code. Let's take a step-by-line look at how they perform:

Line 1th: When this line of code executes, the compiler allocates a small stack of memory for it. The runtime stack is responsible for providing the memory required by the program;

Line 2nd: The program continues execution. Like a name, the stack allocates a chunk of memory at the top of the first memory. You can also think of modules or parts stacked together;

The allocation and deallocation of memory follows the last in, first out (LIFO) logic, in other words, memory can only be allocated or disposed at the top of the I memory block in the example.

Line 3rd: In line 3rd, we created an object. When the row executes, the compiler creates a pointer on the station, and the real object is stored in another memory called "Heap". The heap does not track running memory, it is more like a heap of objects that can be accessed at any time. The heap is used to dynamically allocate memory. It is emphasized here that the reference pointer is allocated on the stack. Declaring Class1 CLS1 does not allocate memory to an instance of Class1, but instead allocates a stack variable CLS1 (and sets to null) and then points it to the heap.

Exit Method: When the method exits, it frees all memory variables on the stack. In other words, all the "Int" variables on the stack are freed according to the last-in-first-out logic. Note that heap memory is not freed at this time, and this memory is later freed by the "garbage collector".

Now there may be a lot of friends wondering why you should allocate 2 kinds of memory instead of just one memory.

If you look closely, you will find that the basic types are not complex, and their values contain simple values such as i=0. Object data types are complex, and they refer to other objects or basic types. In other words, it maintains a variety of other references, and each type must exist in memory. The object type requires dynamic memory and the base type requires static memory. If you need to allocate dynamic memory, allocate it to the heap, and vice versa on the stack.

Value types and reference types

Now that we understand the stack and the heap, look at the value type and the reference type. The data of a value type is in the same location as memory, whereas a reference type is a pointer to memory.

The following example is an shaping data type variable i is assigned to another shaping data type variable J. Their memory values are allocated on the stack. When we assign an int value to another int value, we need to create a completely different copy. In other words, you can change either one without affecting the other.  This data type is called a value type. When we create an object and assign an object to another object, their pointers point to the same memory (for example, when we assign obj to obj1, they point to the same memory). In other words, we change one, which affects the other, which is referred to as the type of reference.

So what kind of type is a value type and a reference type?

In. NET, variables are assigned to the heap or stack, depending on the data type. "String" and "Object" are reference types, and other base types are assigned to the stack and are value types, such as:

Box packing and unpacking

Learning from the above, we learned a lot of useful things, the most useful of which is comes to move data from the stack to the heap when there will be a performance loss. As an example, when we boxed a value type into a reference type, the data is moved from the stack to the heap. Conversely, data is moved from the heap to the stack. This movement between the heap and the stack brings a loss of performance. The process of converting data from a value type to a reference type is called boxing, and vice versa.

If you compile the above code, the IL code in the ILDASM will be sent down how to do the boxing unpacking operation, as follows:

Performance impact of packing and unpacking

To reveal how packing unboxing affects performance, we run the following code 10,000 times. One function has a boxing operation and the other has only simple code. We use a simple timer to see how long they run. The boxing function takes 3542 MS, and the no-boxing operation takes 2477MS of time. This means that in the actual project, boxing and unpacking operations should be avoided unless necessary.

Note:

Recently seen on CodeProject <6 important. NET Concepts:-Stack, heap, Value types, reference types, boxing and unboxing> A article, personally feel very good, so I would like to turn to the students do not want to see English. Due to limited capacity, translation is not good, hope that we can forgive.

6 Important one. NET concepts: stacks, heaps, value types, reference types, boxing, unpacking

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.