Blog with 3 questions to learn: (collation of errors, please help me correct)
CLR: It is responsible for resource management ( memory allocation and garbage collection, etc.) and ensures the necessary separation between the application and the underlying operating system
question 1:What is the three-block area of the CLR (Common Language Runtime Common language runtime ) that manages memory?
question 2: What operations Create objects and allocate memory?
question 3: How is the memory allocation mechanism?
- CLR manages three blocks of memory
Note: Memory -- stack Heap (managed heap)
thread stack: an instance of the value type to assign - There are operating system management allocations to release memory.
Gc . When there is memory allocation, the garbage collector " possibly " Yes
Loh large Object heap 8500byte ) GC
- which operations will Create objects and allocate memory?
IL directive:. NET Intermediate language
Newobj: Reference type Object creation
Ldstr:string type Object creation
Newarr: Array Object creation
Box: value type conversion reference type, value Type field copy to memory allocation on the managed heap
3. allocation mechanism of memory
3.1 The memory allocation mechanism of the stack
For value types: When you are a member of a value type for a class, this time the value type is allocated on the managed heap (heap). Such as:
Classintstring carname;}
namely: Car onecar=new car (); this Car The reference variables of the class are assigned on the thread stack, and the members of this object, such as: Caryear,carname will be allocated on the heap.
for stack variables, which are allocated and freed by the operating system, the operating system maintains a stack pointer pointing to a free space (the starting bit of the unallocated memory). The stack is allocated from high - to-low, while release is from low - to-high, such as:
Static void Main () { int i=1; Char a='a';}
Analysis:
Distribution
The first step: C # program starts from the main function, each thread stack has an initialization address, such as the program's initialization address is 100;
The second step: the int type occupies 4 bytes, then the start stack pointer is at 100, then allocates 4 bytes to the int type (100-97) with the value 1, and then the stack pointer points to 96.
Step three: The char type occupies 2 bytes, then the stack pointer is 96 points to 94.
Step three: When running to the closing parenthesis, the memory will be freed
Release
Fourth step: The step to release is the opposite direction of the assignment, and the stack pointer moves up gradually.
Note: The above approach is more of a "local variable allocation mechanism", because these value type variables end with the end of the method, high efficiency, but small memory capacity.
Memory allocation mechanism for 3.2 heap (managed heap)
For reference types: The memory of a variable of a reference type is allocated on the stack, whereas the memory of an object instance of a reference type is allocated on the managed heap.
There are 2 important areas in the managed heap: GC Heap and load heap (Loader heap)
classProgram {Static voidMain (string[] args) {b b=unll;
b = new B (); B.fild1= One; A A=b; A.fild1= A; Console.WriteLine ("The value of the B-Type B object is: {0}", B.fild1); Console.WriteLine ("the value of a type a object is: {0}", A.fild1); Console.readkey (); } } Public classA { Public intFild1; } Public classb:a { Public intfild2; }
Return to our previous blog to explain the content:
Distribution
The first step: The main function begins, declaring a type B variable, which is just a reference, or a pointer, that is stored on the stack and occupies 4 bytes. At this point, there is no pointer to the object and is initialized to null.
The second step: The new object, which is complicated because the Il (intermediate Code) code is newobj, the process will always look up all its parent classes until the type of object, and this procedure evaluates the type and all fields of the inheritance relationship type, and returns a total number of occupied bytes.
The third step: allocating memory for the stack is advanced after out, to the low expansion, allocating memory is up-down, free memory is up to the bottom-up.
For the heap is FIFO, to high-level expansion. The allocated memory is made up of the bottom-up, which is reclaimed by the GC collector.
For reference types, when the parent class is behind the former subclass, when it finds out that there is not enough memory, it starts the GC collector and reclaims the memory occupied by the garbage object.
Fourth Step: Call the constructor, initialize it, and complete the process of creating the object
Note: For a collection of B objects, the object is recycled instead of curly braces, but until the GC collector reclaims the object
Here's a good explanation of why we output both A and B objects with a value of 12. The process of our conversion does not open up new memory space in the heap. It's always been used. A pointer to a object that changes the value of an object in the heap.
C # memory allocation