. NET heap and stack 01, basic concepts, value type memory allocation,. net01
When we understand some fundamental aspects of the. NET Framework, it is necessary to have a better understanding of the underlying knowledge. For example, how does the. NET Framework manage memory and how does garbage collection... so that we can write programs with higher performance.
In. NET Framework, there are two ways to help us save management data: one is "Heap", also called "managed heap",.. NET Framework's Garbage collector (GC) management; the other is "stack", also known as "thread stack", which is directly managed by the operating system. They are all hosted in the operating system memory.
This article mainly includes:
■ What is "stack"
■ What is "Heap"
■ Value-type memory allocation
※Memory allocation for value types in the Method
※Memory allocation of value types within the reference type
■ Exercise questions
What is "stack"
You can think of a "stack" as a box stacked up from the bottom. Value-type instances are stored here.
Every time A method is called in an application, A box A is put on the heap, the application can only use the box A that is placed at the top of the stack. When the method execution ends, it is equivalent to throwing away the box A at the top. Next, the Box B under A is at the top of the "stack", so the application started to use Box B, and so on. In addition, the memory is automatically released whenever the top box is thrown away.
Stack has high execution efficiency and limited storage capacity.
In. NET Framework, all values derived from System. ValueType are value types, and value type instances are located in the "stack ". Value types include:
● Bool
● Byte
● Char
● Decimal
● Double
● Enum
● Float
● Int
● Long
● Sbyte
● Short
● Stuct
● Uint
● Ulong
● Short
What is "Heap"
You can think of "Heap" as unordered boxes where reference instances are stored.
We can use any box at any time. We need to use the automatic Collection mechanism or manual processing of the Garbage collector (GC) to ensure that the boxes in the "Heap" are recycled in a timely manner.
In addition, according to the size of the reference type instance, the "Heap" is divided into "GC Heap" and "LOH (Large Object Heap) Heap". When the size of the reference type instance is smaller than 85000 bytes, the instance is allocated to the "GC Heap". When the instance size is greater than or equal to 85000 bytes, the instance is allocated to the "LOH (Large Object Heap ".
In. NET Framework, all references derived from System. Object are reference types, and reference type instances are located in "Heap ". Reference types include:
● Class
● Interface
● Delegate
● Object
● String
Value Type Memory Allocation
Value Type memory allocation in the Method
public int Add(int x){ int result; result = x + 2; return result;}
1. Before the Add (int x) method is executed, method parameter x is stored at the top of the stack.
2. Search for the Add () method in "method table". If the Add () method cannot be found, the JIT will be compiled in time and stored in "method table.
3. Start to execute the Add (int x) method. Some memory in "stack" is also required for partial variable result.
4. When the method execution is complete, release result first, then release x, and the thread Stack pointer points again.
Memory Allocation of value types within the reference type
public class MyClass{ public int MyValue;}public MyClass Add(int x){ MyClass result = new MyClass(); result.MyValue = x + 2; return result;}
1. Before the Add (int x) method is executed, method parameter x is stored at the top of the stack.
2. Search for the Add () method in "method table". If the Add () method cannot be found, the JIT will be compiled in time and stored in "method table.
3. Execute the Add (int x) method and execute MyClass result = new MyClass ()
First, create a MyClass instance on the hosting stack, then open up a space on the stack and point to the instance address.
4. When the method execution is complete, the memory is released from top to bottom in the stack.
In this case, what should I do with the MyClass instance on the managed stack?
5. At this time, the garbage collector is on the stage. He searches the managed heap for object instances that are no longer referenced, and then recycles them.
Exercise questions
public int ReturnValue() { int x = new int(); x = 3; int y = new int(); y = x; y = 4; return x; }
The result is: 3. Because the Value Type x variable exists on the stack until the method execution ends.
public int ReturnValue2() { MyInt x = new MyInt(); x.MyValue = 3; MyInt y = new MyInt(); y = x; y.MyValue = 4; return x.MyValue; }
The result is: 4, because when y = x is not assigned to y, the address of x on the hosting stack is actually assigned to y, that is, both x and y on the stack point to the same object instance on the managed stack. Changing the field value of y is equivalent to changing the field value of x.
References:
C # Heap (ing) Vs Stack (ing) in. NET: Part I
NET (version 2nd), Author: Wang Tao.
How does the heap and stack behave in memory?
Java divides memory into two types: stack memory, some basic types of variables defined in the function and referenced variables of objects are allocated in the stack memory of the function. When a variable is defined in a code block, java allocates memory space for the variable in the stack. When the scope of the variable is exceeded, java will automatically release the memory space allocated for changing the amount, and the memory space can be used for another use immediately. Heap memory is used to store objects and arrays created by new. The memory allocated in the heap is managed by the Java Virtual Machine automatic garbage collector. After an array or object is generated in the heap, you can define a special variable in the stack. The value of this variable is equal to the first address of the array or object in the heap memory, this special variable in the stack becomes an array or object reference variable. Later, you can use the reference variable in the stack memory in the program to access the array or object in the stack, the referenced variable is equivalent to an alias or code name for an array or object. The referenced variable is a common variable. When defined, the memory is allocated in the stack. The referenced variable is released outside the scope of the program. Arrays and objects are allocated in the heap. Even if the program runs outside the code block where new statements are used to generate arrays and objects, the heap memory occupied by arrays and objects is not released, arrays and objects become junk only when no referenced variable points to it. They cannot be used again, but still occupy the memory. They are released by the garbage collector at an uncertain time. This is also the main reason why java accounts for memory usage. However, when writing a program, you can manually control it.
If it helps you, please remember to adopt a satisfactory answer. Thank you! Wish you a happy life! Vae. la
Stack and stack are different in memory
All are memory blocks, which are used in different ways. For example, in C language, stack is a data structure, featuring first-in-first-out, which needs to be built by yourself. The heap memory is applied directly using malloc for dynamic memory allocation. The method for creating heap and stack for each language may be different, but the usage is similar. For another example, in win32 compilation, the stack memory does not need to be applied by yourself. The system will automatically allocate a stack segment. You just need to use the stack with push and pop, heap memory has to be applied for by yourself.