简体   繁体   English

.NET中堆栈内存的取消分配

[英]De-Allocation of the Stack memory in .NET

Today, I have read a blog on CodeProject about Memory management .NET. 今天,我在CodeProject上阅读了有关内存管理.NET的博客。

URL - Article 网址- 文章

It says - 它说 -

Exiting the method (the fun): Now finally the execution control starts exiting the method. 退出方法(很有趣):现在,最终执行控制开始退出方法。 When it passes the end control, it clears all the memory variables which are assigned on stack. 通过结束控制后,它将清除在堆栈上分配的所有内存变量。 In other words all variables which are related to int data type are de-allocated in 'LIFO' fashion from the stack. 换句话说,所有与int数据类型相关的变量都以“ LIFO”方式从堆栈中取消分配。

The big catch – It did not de-allocate the heap memory. 最大的收获–它没有取消分配堆内存。 This memory will be later de-allocated by the garbage collector. 稍后,垃圾收集器将重新分配此内存。

As per my understanding, Garbage collector only de-allocate the Heap memory. 根据我的理解,垃圾收集器只会取消分配堆内存。 So, who will de-allocate the stack memory? 那么,谁将取消分配堆栈内存?

Please suggest. 请提出建议。

Values on the stack are automatically managed even without garbage collection because items are added and removed from the stack in a LIFO fashion every time you enter/exit a scope (be it a method or a statement), which is precisely why variables defined within a for loop or if statement aren't available outside that scope. 即使没有垃圾回收,堆栈中的值也可以自动管理,因为每次您输入/退出范围(方法或语句)时,都会以LIFO方式从堆栈中添加和删除项目,这正是为什么在变量中定义变量的原因。 for循环或if语句在该范围之外不可用。

You will receive a StackOverflowException when you've used up all the available space on the stack, though it's almost certainly the symptom of an infinite loop (bug!) or poorly designed system which involves near-endless recursive calls. 用尽堆栈上的所有可用空间后,您将收到一个StackOverflowException,尽管几乎可以肯定是无限循环(错误!)或设计不完善的系统(涉及近乎无限的递归调用)的症状。

In short: 简而言之:

The stackmemory isn't deallocated. 堆栈内存未释放。 It's one block of memory that will be reused. 这是一块将被重用的内存。 Each time a scope declared variables (pushed onto the stack) , it will be popped when the scope exits. 每次作用域声明变量(推送到堆栈上)时,在退出作用域时都会弹出该变量。

So when a method is called, the parameters (a value or a reference pointer) are pushed (copied) onto the stack and popped from it, when the method ends. 因此,在调用方法时,方法结束时,将参数(值或参考指针)压入(复制)到堆栈上并从堆栈中弹出。 (popping is just adjusting a pointer (index) with the memory) (弹出只是调整内存的指针(索引))

That's why variables declared within the { } aren't available behind de } 这就是为什么{ }中声明的变量在de }之后不可用的原因

This chunk of memory is per thread. 这块内存是每个线程的。

In .NET, a variable is located on the stack, regardless of whether it holds a number (a value type), a struct (located entirely on the stack), or a reference to an object (ie the managed address of the object, where the object itself is located on the heap). 在.NET中, 变量位于堆栈上,而不管它是否持有数字(值类型),结构(完全位于堆栈上)或对对象的引用(即对象的托管地址),对象本身在堆上的位置)。

Also, people sometimes confuse variables with class fields. 同样,人们有时会将变量与类字段混淆。 Fields, and all class members, are located on the heap, inside the area allocated when the object was instantiated. 字段和所有类成员位于堆上,在实例化对象时分配的区域内。

So, there are no allocations or deallocations of any variables, since they are just values which go out of scope. 因此,没有任何变量的分配或取消分配,因为它们只是超出范围的值。 After the variable goes out of scope, the GC cannot reach the actual (heap) object and it collects it eventually. 变量超出范围后,GC无法到达实际(堆)对象,并且最终将其收集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM