C# Heap(ing) Vs Stack(ing) in .NET [C# 堆和栈的使用以及垃圾回收原理]

时间:2022-05-25 03:39:12

最近在《C#Corner》上看到了一篇关于.NET内存管理以及垃圾回收的文章,虽说是英文的内容,但还是硬着头皮读了下来。发现并不是我原本想象中的那么枯燥,,因为语言通俗而且还有很多图片示意,感觉让我又对”堆“和”栈"以及垃圾回收机制有了更加深刻的理解和认知,记录下来提醒自己尽量书写优质的代码,而不是只管实现功能,不管性能优劣去蛮干。  【文章出自: 

文章正文如下:

第一部分

Even though with the .NET framework we don‘t have to actively worry about memory management and garbage collection (GC), we still have to keep memory management and GC in mind in order to optimize the performance of our applications. Also, having a basic understanding of how memory management works will help explain the behavior of the variables we work with in every program we write.  In this article I‘ll cover the basics of the Stack and Heap, types of variables and why some variables work as they do.

There are two places the .NET framework stores items in memory as your code executes.  If you are not yet familiar with them, let me introduce you to the Stack and the Heap. Both the Stack and Heap help us run our code. They reside in the operating memory on our machine and contain the pieces of information we need to make it all happen.

Stack vs. Heap: What‘s the difference?

The Stack is more or less responsible for keeping track of what‘s executing in our code (or what‘s been "called").  The Heap is more or less responsible for keeping track of our objects (our data, well... most of it; we‘ll get to that later).

Think of the Stack as a series of boxes stacked one on top of the next.  We keep track of what‘s going on in our application by stacking another box on top every time we call a method (called a Frame).  We can only use what‘s in the top box on the Stack.  When we‘re done with the top box (the method is done executing) we throw it away and proceed to use the stuff in the previous box on the top of the Stack. The Heap is similar except that its purpose is to hold information (not keep track of execution most of the time) so anything in our Heap can be accessed at  any time.  With the Heap, there are no constraints as to what can be accessed like in the Stack.  The Heap is like the heap of clean laundry on our bed that we have not taken the time to put away yet; we can grab what we need quickly. The Stack is like the Stack of shoe boxes in the closet where we have to take off the top one to get to the one underneath it.

C# Heap(ing) Vs Stack(ing) in .NET [C# 堆和栈的使用以及垃圾回收原理]


The picture above, while not really a true representation of what‘s happening in memory, helps us distinguish a Stack from a Heap.
 
The Stack is self-maintaining, meaning that it basically takes care of its own memory management.  When the top box is no longer used, it‘s thrown out.  The Heap, on the other hand, must worry about Garbage collection (GC), which deals with how to keep the Heap clean (no one wants dirty laundry laying around, it stinks!).

What goes on the Stack and Heap?

We have four main types of things we‘ll be putting in the Stack and Heap as our code is executing: Value Types, Reference Types, Pointers, and Instructions

Value Types:

In C#, all the "things" declared with the following list of type declarations are Value types (because they are from System.ValueType):

bool

byte

char

decimal

double

enum

float

int

long

sbyte

short

struct

uint

ulong

ushort

Reference Types:

All the "things" declared with the types in this list are Reference types (and inherit from System.Object, except, of course, for object which is the System.Object object):

class

interface

delegate

object

string

Pointers:

The third type of "thing" to be put in our memory management scheme is a Reference to a Type. A Reference is often referred to as a Pointer. We don‘t explicitly use Pointers, they are managed by the Common Language Runtime (CLR). A Pointer (or Reference) is different than a Reference Type in that when we say something is a Reference Type, it means we access it through a Pointer. A Pointer is a chunk of space in memory that points to another space in memory.  A Pointer takes up space just like any other thing that we‘re putting in the Stack and Heap and its value is either a memory address or null.