What is Stack heap
In the computer field, stacks are a concept that cannot be ignored, and stacks are two kinds of data structures. A stack is an ordered data structure of data items that can be inserted and deleted only at one end (called the top of the stack). Points: Heap, queue first, FIFO (Fifo-first in First out), stack, advanced post-out (filo-first-in/last-out).
A stack is an abstract data type that is often used in computer science. The object in the stack has an attribute: The last object placed in the stack is always first taken out, and this feature is often called a LIFO queue.
Stack and heap differences stacking space allocation
Stack (operating System): automatically allocated by the operating system release, the value of the variables that hold the function, the value of the local variables, and so on, its operation is similar to the stack in the data structure;
Heap (operating system): Generally by the developer allocation release, if not released, the program ends may have an OS recovery, distribution is similar to the list;
How stacks are Cached
The stack uses a first-level cache, usually in storage when it is called, and is automatically released after the call is completed;
The heap uses a level two cache, and the life cycle is determined by the garbage collection algorithm of the virtual machine (which is not necessarily an orphan object to be released immediately). So the speed at which these objects are called is relatively slow;
Differences in stack data structures
Heap (data structure): Heaps can be regarded as a tree; For example: heap sort;
Stack (data structure): An advanced post-out data structure;
Difference Introduction
The stack is responsible for saving our code to execute (or call) the path, while the heap is responsible for saving the paths of the object (or the data).
You can think of stacks as a stack of boxes stacked from top to bottom. When each method is called, we record what is going to happen in the application in a box at the top of the stack, and we can only use that box at the top of the stack at a time. When the box on top of our stack is used, or after the method has been executed, we will throw the box away and continue using the new box on top of the stack. The heap works similarly, but most of the time the heap is used to save information rather than save the execution path, so the heap can be accessed at any time. There are no access restrictions on the heap compared to stacks, and heaps are like old clothes on the bed, and we don't take the time to sort it out because we can always find the clothes we need, and the stacks are like the boxes stacked in the locker, and we can only start with the top-level boxes until we find the right one.
Slices are not real-world representations of memory, but can help us differentiate stacks and heaps.
The stack is self-sustaining, which means that the memory automatically maintains the stack, and when the stack top box is no longer used, it will be thrown. On the contrary, the heap needs to consider garbage collection, garbage collection is used to keep the heap neat, no one wants to see all the clothes around, it stinks!
When our code executes, there are four types of data in the stack and heap: The value type, the reference type (Reference type), the pointer (Pointer), the directive (instruction).
1. Value types
Bool,byte, Char,decimal,double,enum,float,int,long,sbyte,short,struct,uint,ulong,ushort
2. Reference types
Class,interface,delegate, Object, string
3. Pointers
The third type that is placed in a memory management scenario is a type reference, which is usually a pointer. We do not display the use pointers, which are managed by the common language runtime (CLR). A pointer (or reference) is different from a reference type, because when we say that something is a reference type, it means that we are accessing it through pointers. The pointer is a piece of memory space, and it points to another memory space. Just like stacks and heaps, pointers also take up memory space, but their values are either a memory address or empty.
such as, can help us better understand the heap and stack, in the box and unboxing can also reflect
Stack usage
1. Reference types are always placed in the heap.
2. Value types and pointers are always placed where they are declared.
As we mentioned earlier, the stack is the path that is responsible for saving our code when it executes (or calls). When our code starts calling a method, it places a coded instruction (in the method) onto the stack, followed by the parameters of the method, and the code executes to the variable position in the method that is "stacked" to the top of the stack. It is easy to understand through the following examples ...
Define a method
Public int Addfive (int pValue) { int result; 5 ; return result; }
Now take a look at what's going on at the top of the stack, and remember that the top of the stack we're looking at actually has a lot of other stuff in it.
The first method (contains only the logical bytes that need to be executed, that is, the instruction that executes the method, not the data in the method body) into the stack, followed by the method's parameters into the stack.
Next, the control (that is, the thread that executes the method) is passed to the addfive () command on the stack
When the method executes, we need to allocate some memory on the stack for the "result" variable.
The method execution is completed, and the result of the method is returned.
By pointing the stack pointer to the available memory address used by the addfive () method, all memory used by the method on the stack is emptied, and the program automatically returns to the location of the original method call on the stack.
In this example, our "result" variable is placed on the stack, in fact, when the value type data is declared in the method body, they are placed on the stack.
Value type data is also sometimes placed on the heap. Remember this rule--value types are always placed where they are declared. Well, if a value type data is declared outside the method body and exists in a reference type, it will be superseded by the reference type in the heap.
See another example:
//If we have such a myint class (it is a reference type because it is a class type): Public classMyInt { Public intmyvalue; } //then execute the following method: PublicMyInt addfive (intpValue) {MyInt result=NewMyInt (); Result. MyValue= PValue +5; returnresult; }
As mentioned earlier, the parameters of the method and method are placed on the stack, and then the control is passed to the addfive () command on the stack.
Then there are some interesting phenomena ...
Because "MyInt" is a reference type, it is placed on the heap and a pointer reference to the heap is generated on the stack.
After the Addfive () method is executed, we will empty ...
We will leave the lonely Myint object in the heap (there will be no pointers to the Myint object in the stack!)
This is where the garbage collector (hereafter referred to as GC) works. When our program reaches a specific memory threshold, we need more heap space when the GC starts to work. The GC stops all running threads, finds all the objects that exist in the heap that are no longer accessible to the main program, and deletes them. The GC then re-organizes all the remaining objects in the heap to save space and adjusts all the pointers associated with those objects in the stack and heap. You'll think that this process is very expensive, so you'll know why we need to focus so much on stacks and heaps, especially when it comes to writing high-performance code.
When we use reference types, we are actually dealing with pointers of that type, not the type itself. When we use value types, we are using the value type itself.
Case:
Perform the following methods
public int returnvalue () { int x = new int (); x = 3; int y = new int (); y = x; y = 4; return x; }
The final result is 3
If we first use the Myint class
Public classMyInt { Public intmyvalue; } //The following methods are then performed: Public intReturnValue2 () {MyInt x=NewMyInt (); X.myvalue=3; MyInt y=NewMyInt (); Y=x; Y.myvalue=4;//The heap is operated at this timereturnX.myvalue; }
The final result is 4
Why? How can x.myvalue become 4? ... Look what we've done, and we'll know what's going on:
In the first example, everything goes as planned:
In the second example, we don't get "3" because the variable "x" and "Y" both point to the same object in the heap.
. NET stack and heap details