Six important. NET concept stacks, heaps, value types, reference types, packing, unpacking, And. net packing

Source: Internet
Author: User

Six important. NET concept stacks, heaps, value types, reference types, packing, unpacking, And. net packing
Introduction

This article will explain six important concepts: Stack, heap, value type, reference type, packing, and unpacking. This article will explain what happens When you declare a variable and explain two important concepts in advance: Stack and heap. This article will clarify some important basic information about the reference type and value type. A simple example is provided to demonstrate the performance loss caused by packing and unpacking.

 

What happens when a variable is declared?

When you are in. when a variable is declared in the. NET application, a small block of memory will be allocated from RAM. There are three things in the memory: variable name, data type, and variable value.

Let's briefly describe what happened in the memory. You must know that the allocation of variables in the memory depends on the data type. There are two types of memory allocation: stack memory and heap memory. As shown in, we will have a clearer understanding of these two types of memory.

 

Stack and stack

To understand the heap and stack, let's take a look at how the following code runs internally.

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

There are three lines of code. Let's explain its internal mechanism in one line.

The first line:After this line of code is executed, the compiler allocates a small block of memory on the stack, and the stack is responsible for tracking the running of the application in the memory.

Row 2:Execute the next step. As the stack name says, each memory allocation is at the top of the stack. You can think of the stack as a series of stacked boxes.

Memory Allocation and recovery are implemented through the LIFO (first-in-first-out) logic. In other words, the memory allocation and recovery start from the top.

Row 3:In row 3, an object is created. After this line of code is executed, a pointer is created on the stack to point to this object, and the object is actually stored in another

It is called "heap" in different memory locations. The heap cannot track the running of memory. It is only a collection of objects that can be accessed at any time. The heap is used for dynamic memory allocation.

Note that the reference pointer is allocated to the stack. Declare Class1 cls1; no memory is allocated to class Class1. It allocates only one cls1 stack variable (and sets it to null) until it encounters a new keyword.

Exit method (function): Now you can exit the execution control method. When it ends the final control, it clears all memory variables allocated on the stack. In other words, all the variables related to the int data type are recycled from the stack in the "first-in-first-out" mode.

Note that the heap does not release the memory. Instead, it is recycled by the garbage collector.

 

Now many developers are wondering why there are two types of memory. We can't just allocate one memory type to achieve everything we do? If you observe carefully, the basic data types are not complex and they hold a single value, such as "int I = 0 ". Object data types are complex. They reference other objects or other basic data types. In other words, they have pointers referenced by multiple other values, and each pointer must be stored in memory. The Object type requires dynamic memory while the basic type requires static memory. If dynamic memory is required, it must be allocated to the stack; otherwise, it is allocated to the stack.

 

Value Type and reference type

Now that we have understood the concept of stack and stack, it is time to understand the concept of value type and reference type. The Value Type stores data and memory in the same location, while a reference type has a pointer to the actual memory area.

We can see that the value of an integer data type named I is assigned to another Integer Data Type named j. Their values are stored on the stack.

When we assign a value of the int type to another value of the int type, it actually creates a completely different copy. In other words, if you change one value, the other will not change. Therefore, the data types of these types are called "value types ".

 

When we create an object and assign it to another object, they all point to the same area of memory as shown in the code segment. Therefore, when we assign obj to obj1, they all point to the same area in the heap. In other words, if we change any of them at this time, the other will be affected, which also shows why they are called "reference type ".

What are value types and reference types?

In. NET, whether a variable is stored in the stack or heap depends entirely on its data type. For example, 'string' or 'object' belongs to the reference type, while other. NET base metadata types are allocated to the stack. It details the values and references in the. NET preset types.

 

Packing and unpacking

Now you have a lot of theoretical basics. Now, it is time to understand how to use the above knowledge in actual programming. The biggest significance of an application is to understand the performance consumption problems in the process of moving data from Stack to stack, and vice versa.

Consider the following code snippet. When we convert a value type to a reference type, the data will be moved from the stack to the heap. On the contrary, when we convert a reference type to a value type, the data will also be moved from the heap to the stack.

System performance is inevitably affected, whether it is moving from Stack to stack or from Stack to stack.

As a result, two new terms emerged: when the data is converted from the value type to the reference type, it is called "Packing", and the process from the reference type to the value type is changed to "unpacking ".

 

If you compile the above Code and view it in ILDASM (an IL decompilation tool), you will find out what it looks like in the IL code. The IL code generated after the sample code is compiled is displayed.

 

Packing and unpacking performance problems

To understand the performance impact of packing and unpacking, We cyclically run the two function methods shown 10000 times. The first method contains the packing operation, while the other method does not. We use a Stopwatch object to monitor time consumption.

The method with a boxing operation took 3542 milliseconds to complete, while the method without a boxing operation only took 2477 milliseconds, a full difference of more than 1 second. This value also increases with the increase in the number of loops. That is to say, we should try to avoid packing and unpacking. In a project, if you need to pack and pack, consider whether it is absolutely essential. If not, try not to use it.


Although the above Code segment does not show the box-breaking operation, the effect is also applicable to the box-breaking operation. You can write code to split the box and use Stopwatch to test the time consumption.

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.