About memory allocation in C #

Source: Internet
Author: User

Programmers who have a little experience with C # know that the value type and reference type are allocated differently in the memory. Next we will talk about their specific allocation process.

1. Value Type allocation.

There is a stack area in the virtual memory. We don't know where it is in the address space. We do not need to know it during general development, we know that the value type is assigned here. When the value type is allocated on the stack, it is filled from top to bottom, that is, starting from the high memory address.

For example, the current stack pointer is 100000, which indicates that its next free bucket starts from 99999. When we declare an int type variable A in C, because the int type is four bytes, it will be allocated to the storage unit from 99996 to 99999. If we declare the double variable B (8 bytes), the variable will be allocated to the storage unit from 99988 to 99995. If the code runs out of their scope, both variables A and B will be deleted. In this case, the order is the opposite. Delete variable B first, and the stack pointer will increase by 8, that is, point to the position 99996 again. Then, delete variable A and point the stack pointer to 10000 again. If two variables are declared at the same time. For example, int A and B, we do not know the allocation order of A and B, but the compiler will ensure that their deletion order is exactly the opposite of the allocation order.

It can be seen that for simple value types, their allocation methods are well understood.

 

2. Allocate a reference type.

After learning about the allocation method on the stack, it is obvious that its performance is quite high. At the same time, we also find a drawback: the lifetime of the variable must be nested. This is unacceptable in some cases. Sometimes we need to store some data and the data can still be used after the method exits. To this end, the virtual memory is allocated to another part of the region, which is called the managed heap. A major difference between managed heap and traditional heap is that the managed heap works under the control of the garbage collector. The reference type is assigned to the managed stack. Let's take a look at the allocation process of the reference type.

Suppose we need to declare a person class and instantiate it.

Person P = new person ();

First, the system will allocate storage space to the variable P on the stack. Of course, it is just a reference to store the location of the person instance on the managed stack, there is no real person instance. Because it only stores an address (an integer), it occupies 4 bytes of space on the stack. The person instance will be stored on the managed stack. Different from the stack, the managed stack is allocated from the bottom up. Assume that the instance occupies 10 bytes, and assume that the address on the managed stack is 200000, then it will be allocated to the storage unit from 200000 to 200009.

It should be noted that this allocation is related to the instance size. If the instance is smaller than 85000 bytes, it will be allocated in the managed heap. If it exceeds 85000 bytes, it will be allocated to LOH.

It can be seen that the allocation method of the ratio type in this allocation process is more complex, so there is an inevitable performance loss. This is why we prefer to use structures instead of classes for small data structures.

 

Of course, these are relatively simple allocation methods, and the actual situation may be more complicated than this. For example, define a class instance in struct. This type of instance is allocated to the managed heap, and its address is allocated to the stack. If a value type int variable is defined in the class, the value of the variable will be allocated to the managed stack instead of the stack.

 

 

 

 

 

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.