如何垃圾收集器会表现的值类型和引用类型 [英] How Garbage collector Will behave on value type and Reference type

查看:104
本文介绍了如何垃圾收集器会表现的值类型和引用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何垃圾收集器会表现的值类型和引用类型的时候,就会释放内存的值类型和引用类型。我感到困惑这一点任何人都可以了解这个解释,同时垃圾收集器删除值类型和引用类型的自由内存,先删除

How Garbage collector Will behave on value type and Reference type, when it will free memory value type and reference type. i am little bit confused about this can anyone explain about this, at the same time garbage collector remove free the Memory of value type or Reference type Which remove first

推荐答案

您正在思考这个问题的错误的方式。停止思考值类型和引用类型。相反,开始思考的变量的那些变量,无论是的短暂长寿命的。

You're thinking about the problem the wrong way. Stop thinking about "value types" and "reference types". Instead, start thinking about variables, and whether those variables are short lived or long lived.

垃圾回收堆的目的是为了回收与长寿相关的变量存储。的宗旨的的是回收与短期关联的存储住变数。

The purpose of the garbage collected heap is to reclaim the storage associated with long-lived variables. The purpose of the stack is to reclaim the storage associated with short-lived variables.

人们会试图告诉你,值类型进入堆叠和引用去堆等等,这是令人困惑的无稽之谈。 变量走栈或堆(或寄存器 - 每个人都忘掉寄存器)和变量可以是值类型或引用类型。

People will try to tell you that "value types go on the stack" and "references go on the heap" and so on, and this is confusing nonsense. Variables go on the stack or the heap (or registers -- everyone forgets about registers) and variables can be of value type or reference type.

您一直在问,这将垃圾收集器首先删除?这个问题不能回答。垃圾回收堆不保证任何有关其内存被回收的顺序。短暂的存储 - 堆栈 - 为激活帧弹出堆栈将被回收。 但是,C#语言允许垃圾收集清理由短暂的存储帧被弹出之前堆栈如果运行时能够确定的基准不会再次访问引用的存储空间。基本上,的当存储的回收是运行时的实现细节,可能在任何时刻的改变。

You keep asking "which will the garbage collector remove first?" That question cannot be answered. The garbage collected heap makes no guarantees whatsoever about the order in which memory is reclaimed. The short-lived storage -- the stack -- will be reclaimed as activation frames are popped off the stack. However, the C# language permits the garbage collector to clean up storage that is referenced by short-lived storage before the frame is popped off the stack if the runtime can determine that the reference will not be accessed again. Basically, when storage is reclaimed is an implementation detail of the runtime, subject to change at any moment.

这篇关于如何垃圾收集器会表现的值类型和引用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆