Home >Backend Development >C#.Net Tutorial >Introducing the heap and stack in C#

Introducing the heap and stack in C#

巴扎黑
巴扎黑Original
2017-09-06 11:16:001701browse

1. What is a heap?

The heap is a memory area in which large blocks of memory can be allocated to store certain types of data objects;

Characteristics of the heap:

1. Inside the heap The memory can be stored and removed in any order;

 2. The data saved by the program in the heap cannot be deleted explicitly;

 3. Rely on the CLR's automatic GC (garbage collector) Determine and automatically clear the unowned heap object, and then release it;

 4. The memory of the released object can be reused;

2. What is the stack?

The stack is a memory array and a last-in-first-out data structure;

Several data types:

1. The values ​​of certain types of variables;

  2. The current execution environment of the program;

  3. Parameters passed to the method; Insertion and deletion at the top;

2. Putting data on the top of the stack becomes a push;

3. Deleting data from the top of the stack becomes a pop;

3. Stack and What's the difference?

1. The stored data types are different;

2. The stack can only be inserted and deleted from the top, while the heap can be stored and removed in any order;

3. Stack memory does not need to be managed and is not managed by GC. When the top element of the stack is used up, it is released immediately. The heap needs to be cleaned by GC (Garbage collection: garbage collector);

4. The stack is automatically allocated by the system and is faster. But there is no control. The heap is the memory blocks allocated by new. The compiler does not care about their release and is controlled by our application. It is generally slow and prone to memory fragmentation.

The above is the detailed content of Introducing the heap and stack in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn