C # in the stack and heap

The program is running, its data must be stored in memory. A data item require much memory, what position is stored in memory, and how to store all depends on the type of the data item.

Running program using two memory areas to store data: stack and heap.

Stack

It is a memory array of the stack is a LIFO (Last-In First-Out, LIFO) data structure. Several types of data stored in the stack:

  • Some types of variable values
  • The current program execution environment
  • Parameters passed to the method

The system manages all stack operations. As a programmer, you do not need to explicitly do anything to it. But understanding the basic functions of the stack can better understand what the program is doing at runtime.

Tip: Stack also often referred to as a stack, heap and stack still called, so this concept does not include heap stack, we should pay attention to distinguish.

Stack feature

Stack has the following common features:

  • Data can only insertion and deletion from the top of the stack
  • The data stack into the top of the stack is called (push)
  • Called stack data is deleted from the stack (pop)
Pushing and popping

stack

The heap is an area of ​​memory may be allocated chunk of memory for storing certain types of data objects in the heap. With different stacks, heap memory and the memory can be removed in any order. The following figure shows the pile put in a four program data.

Heap

Although the program can save the data in the heap, but it does not explicitly delete them. When the CLR Automatic GC (Garbage Collector, garbage collector) determines that the program code will no longer access a data item, automatically cleared heap objects ownerless. We therefore can not worry about this when using the C programming language is very error-prone work. The figure below illustrates the garbage collection process.

Automatic garbage collector heap

to sum up

This introduction of the stack and heap the most basic concepts, primarily for Next (address: https://www.vinanysoft.com/c-sharp-basics/data-types/categories-of-types/ ) introduce value types and reference types laying the foundation.

Guess you like

Origin www.cnblogs.com/vin-c/p/12043984.html