Explanation of the six important concepts that you must know in. Net (graphic explanation)-Conversion

Source: Internet
Author: User

Address: http://developer.51cto.com/art/201005/197729.htm

 

[This article will explain the six important concepts in. Net, which are stack, heap, value type, reference type, packing and unpacking. First, explain what will happen in the memory when you declare a variable, then introduce two important concepts: Stack and heap, and then introduce the value type and reference type, finally, the article uses examples to introduce the impact of packing and unpacking on performance.]

 

What happens in the memory when you declare a variable?

When you are in. when declaring a variable in A. NET application, you must first allocate some memory to the ram, which includes three items: the first is the variable name, and the second is the data type of the variable, the last one is the variable value.

This is just a simple explanation. There are two types of memory allocation depending on the Data Type of the variable: stack memory and heap memory.

 

Figure 2 memory structure after variables are declared

Stack and heap)

To help understand the stack and heap, Let's know what happened inside the code below.

 
 
  1. public void Method1()  
  2. {  
  3. // Line 1  
  4. int i=4;  
  5. // Line 2  
  6. int y=2;  
  7. //Line 3  
  8. class1 cls1 = new class1();  

There are only three lines of code in this method. Next I will explain what happened inside the method one by one.

Row 1: When this row is executed, the compiler allocates a small block of memory called the stack, which keeps track of the memory required by the application program.

Row 2: Now move the execution to the next step. As the stack name implies, this memory allocation is stacked at the top of the previous memory allocation, you can think of a stack as a series of bins or boxes stacked layer by layer.

The last in first out logic is used for memory allocation and unallocation. In other words, the memory is allocated and unallocated at the end of the memory (such as the top of the stack.

Row 3: In row 3, we create an object and execute this row. It creates a pointer on the stack. The actual object is to store a different type of memory allocation (called heap) the heap does not track the running memory, but only the accumulation of objects. The heap is used for dynamic memory allocation.

Exit method (interesting): exit this method after the last line of code is executed. when it passes the end control, it clears all memory variables allocated to the stack, in other words, all the variables associated with the int data type are removed from the stack in LIFO mode.

But the heap memory allocation will not be removed. This part of the memory needs to be unallocated through the garbage collector (Garbage Collector.

 

Figure 3 Memory Internal Operations corresponding to the three lines of code

Many may ask why two memory allocation modes should be set? Isn't it possible to use a memory allocation form to complete memory allocation?

If you observe it carefully, you will know that int variables are distributed on the stack, because the compiler already knows how much data they can store (-2,147,483,648 to 2,147,483,647), when it comes to objects, the compiler does not know how much internal space is required, so it allocates the same size space on the stack.

In other words, if you do not know the data size or dynamic changes, you need to allocate the data to the stack. If the data size is determined, it is allocated to the stack.

 

 

Figure 4 the variable size is allocated to the stack in an hour, and the variable size is allocated to the stack in an hour.

Value Type and reference type

The value type refers to the data and memory types that are simultaneously accommodated at the same location, and the reference type is directed to the memory location by a pointer. The following is a simple Integer Data Type named I. Its value is assigned by another Integer Data Type named J. These two memory values are allocated based on stacks.

When we assign an int value to another int value, it creates a completely different copy. In other words, if you modify one value, it will not change the other value, this type of data is called the value type.

 

 

Figure 5 value type: A value change does not cause another value change

When we assign an object to another object, they point to the same memory location, as shown in. When we assign OBJ to obj1, they point to the same memory location. In other words, if one of the objects is modified, another object will also be affected. This type is called the reference type.

 

 

Figure 6 reference type: changes to one object may cause changes to another object

Which data type is the value type and reference type?

In. in. net, variables may be stack-based or heap-based, string and objects are reference types, and others.. NET data types are stack-based and explained in more detail.

 

Figure 7. The reference type and value type correspond to the Data Type in. net.

 

Boxing and unboxing)

After talking about this, how can we use them in actual programming? The biggest problem is figuring out the performance loss of data moving from the stack to the heap, and vice versa.

As shown in, when we move a value type to the reference type, the data is also moved from the stack to the heap. When we move the reference type to the value type, the data is moved from the stack to the stack. Moving data from a stack to a stack, or from a stack to a stack, results in high performance loss. The process of moving data from the value type to the reference type is called packing, and moving from the reference type to the value type is called unpacking.

 

 

Figure 8 packing and unpacking process diagram

If you compile the above Code, in the same ildasm, you will see how the code in Il is packed and unpacked, as shown in.

 

Figure 9 packing and unpacking

Effects of packing and unpacking on Performance

To view the impact of performance, we run the following two functions for 1000 times, as shown in. The left function has a box unboxing operation, and the Right function does not, we used a stopwatch object monitoring time.

 

Figure 10 compare the running time of case unboxing and case unboxing

As we can see, it takes 3542 milliseconds to deploy and unpack a box without decoration, and only 2477 milliseconds to deploy the box without decoration. Therefore, the impact on performance is quite large.

Do you understand these two important. Net concepts?

Original article name: 6 important. Net concepts:-stack, heap, value types, reference types, boxing and unboxing.

 

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.