Measure the test taker's knowledge about how to allocate storage resources to variables in memory. (1)

Source: Internet
Author: User
Tags call back
1. Preface

In code writing, we often call back one method or another (function). But do we know how to allocate various parameter types in memory? Not necessarily. At least I didn't know much about it before. We may think that this is only a small problem. We feel that we can only know what the method is and call the method, so we ignore this. The following describes how the parameters in the method are allocated in memory. If anything is wrong, please give me some advice.

2. Value Type and reference type allocation

Generally, the value type is allocated to the stack, while the reference type is allocated to the storage unit in the heap. The stack allocates memory space during compilation, which is clearly defined in the Code, and the heap is the memory space dynamically allocated in the program running, the memory size can be dynamically allocated based on the program running status. Therefore, the value type always occupies a predefined number of bytes in the memory (for example, int occupies 4 bytes, that is, 32 bytes ). When a value type variable is declared, the memory space occupied by the variable type is automatically allocated in the stack and the values contained in the variable are stored .. Net automatically maintains a stack pointer, which contains the address of the next available memory space in the stack. The stack is first imported and then output, and the top variables in the stack always leave the scope before the following variables. When a variable leaves the scope, the stack pointer moves down the number of bytes occupied by the released variable and still points to the next available address. Note: value-type variables must be initialized during use.

Variables of the reference type are allocated with a memory space in the stack. This memory space stores references to another memory location, which is an address in the managed heap, that is, the place where the actual value of this variable is stored .. Net also automatically maintains a heap pointer, which contains the address of the next available memory space in the heap, but the heap does not come in and out first, objects in the heap do not leave the scope at a predefined point of the program. In order to release the object without using the heap memory ,.. NET will periodically perform garbage collection. The garbage collector recursively checks all object references in the application. When it finds that the reference is no longer valid, the memory used by the object cannot be accessed from the program, this memory can be recycled (except for objects fixed in memory by the fixed keyword ).

However, the value type allocates memory on the stack, while the reference type allocates memory on the managed stack. The following is a more detailed and accurate description found on the Internet:

1. For a value-type instance, if it is a local variable in the method, it is created on the thread stack. If the instance is a member of the class type, it is part of the type member, other types of fields are stored on the managed stack;
2. An instance of the reference type is created on the managed stack. If its byte is smaller than 85000 bytes, it is directly created on the managed stack; otherwise, it is created on LOH (large objet heal.

For storage of reference types, I think the second point above is not detailed enough. If the reference type is not a member of a class type, its storage is divided into two parts, one part is the reference part, which is stored on the stack and points to the specific heap address, and the other part is the actual data part, which is stored on the stack. If the reference type is a member of the class type, therefore, both the reference part and the data part are stored on the stack as class instances.

1 public class myclass2 {3 private int I; // even if the variable I is a value type, it is used as part of the myclass (reference type) instance, the instance with myclass is created on the GC stack 4 Public myclass () 5 {6 Int J = 0; // as a local variable, j's instance is created on the thread stack where the code is executed 7} 8}

 

3. Memory Allocation for the value type and reference type of the array

The array is divided into value type and reference type. See the following code snippet:

 1 public class Person 2 { 3     private int age; 4     private string name; 5      6     public int Age 7     { 8         get; 9         set;10     }11     12     public string Name13     {14         get;15         set;16     }17 }
1 person [] person = new person [5]; // reference type array 2 int [] num = new int [5]; // value type array


The array is also a reference type, so the variables of the array type are allocated to the stack.

For the int Type above (simple value type array), each array member is a reference (pointer ), space referenced to the stack (because the memory of the value type variable is allocated to the stack );

For the above person class type (reference type, class type array), each array member is still a reference (pointer ), space referenced to the stack (because the memory of the class instance is allocated to the stack)

4. Summary

I have been reading this information recently. I always feel that writing it will help to deepen my impression. On the other hand, I can provide new ideas for beginners. The above is the memory allocation method for the value type and reference type. If there is anything wrong, please point out that I will learn and modify it accordingly.

In the next article, I will introduce the memory allocation methods for specific method parameters. Thank you !!

 

 

Measure the test taker's knowledge about how to allocate storage resources to variables in memory. (1)

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.