问题描述
我知道引用类型将被垃圾回收.我想知道值类型是否也会从堆栈中被垃圾回收?
I know that reference type will be garbage collected. I wanted to know whether value types will also be garbage collected from the stack?
推荐答案
很不清楚你的问题是什么意思.你能仔细定义什么是收集的垃圾"吗?方法?它的意思是是 GC 算法的输入",还是通过压缩 GC 堆来释放",还是什么?
It is very unclear what your question means. Can you carefully define what "garbage collected" means? Does it mean "are inputs to the GC algorithm", or "are deallocated by compacting the GC heap", or what?
存储在堆栈中的值——无论是值类型的值还是引用类型的值——都是集合算法的根.它们没有被收集,因为它们是有生命的东西,几乎可以让其他一切都保持活力.
Values stored on the stack -- whether values of value types or reference types -- are the roots of the collection algorithm. They are not collected because they are the things that are alive that keep almost everything else alive.
显然,它们并没有通过压缩 GC 堆来释放;它们通过弹出堆栈被释放.
And obviously they are not deallocated by compacting the GC heap; they're deallocated by popping the stack.
这能回答你的问题吗?
更新:
我所说的垃圾收集"是什么意思?是也就是说,如果一个值类型变量是发现不被使用应用程序然后它将被删除从堆栈中
What I mean by "garbage collected" is that, if a value type variable is found not to be used by the application then it will be removed from the stack
好的,我认为我们正在接近一个可以回答的问题.现在我们需要了解您所说的从堆栈中移除"到底是什么意思.
OK, we're getting closer to an answerable question here I think. Now we need to understand what exactly you mean by "removed from the stack".
堆栈是一块大小为一百万字节的预分配内存.有时我们使用该内存块的一部分来存储值类型的局部变量.从堆栈中删除"究竟是什么意思?堆栈的大小永远不会改变;它是一百万字节的预分配内存块.
The stack is a block of pre-allocated memory one million bytes in size. Sometimes we use portions of that block of memory to store local variables of value type. What precisely do you mean by "removed from the stack"? The stack never changes in size; it's a one-million-byte block of pre-allocated memory.
堆栈分为两个连续的区域,我们将其称为有效"区域.和无效"堆栈的部分.在 x86 架构上,ESP 寄存器指向这些区域之间的边界.您是在问在什么情况下,与堆栈上的特定值类型的局部变量相关联的内存会成为基于 x86 架构上 ESP 寄存器值变化的无效部分的一部分?"
The stack is divided into two contiguous regions, which we'll call the "valid" and "invalid" sections of the stack. On x86 architectures the ESP register points to the boundary between those regions. Are you asking "under what conditions does the memory associated with a particular local variable of value type on the stack become a part of the invalid section based on a change in value of the ESP register on x86 architectures?"
这似乎是一个非常、非常实施细节";你的问题的版本.堆栈是运行时特定版本的实现细节,所以如果你要问关于它的问题,你将不得不接受这样一个事实,即你问的是一个特定芯片架构上特定寄存器中的特定值.
This might seem like a very, very "implementation detail" version of your question. The stack is an implementation detail of a specific version of the runtime, so if you're going to ask questions about it, you're going to have to accept the fact that you're asking about a specific value in a specific register on a specific chip architecture.
进一步阅读:
引用不是地址
堆栈是一个实现细节,第一部分
堆栈是一个实现细节,第二部分
内存不足"不指物理内存
我现在读到你提到的关于价值观"的内容有点困惑.和值类型".我发现很难理解其中的区别.
I am a bit confused now to read what you have mentioned about "values" and "value types". I am finding it hard to understand the difference.
这很棘手!我们使用价值"这个词.和参考"意味着太多的东西.让我总结一下.
It is tricky! We use the words "value" and "reference" to mean too many things. Let me sum up.
变量是一个存储位置.
每个变量都有一个类型.类型可以是值类型或引用类型.
Every variable has a type. A type can be a value type or a reference type.
一个存储位置包含一个值.
A storage location contains a value.
值类型变量的值是值类型的值.例如,int 是一个值类型.int 类型变量的值是 int,例如 12.
The value of a variable of value type is a value of the value type. For example, int is a value type. The value of a variable of type int is an int, say, 12.
引用类型变量的值是对该类型对象的引用,或者为 null.例如,字符串是引用类型.string 类型变量的值是对字符串的引用,或者为 null.
The value of a variable of reference type is a reference to an object of that type, or null. For example, string is a reference type. The value of a variable of type string is a reference to a string, or null.
这就是为什么它们被称为值类型"的原因.和参考类型".值类型的值是该类型的一个实例.引用类型的值是对该类型实例的引用.
That's why they're called "value types" and "reference types". The value of a value type is an instance of the type. The value of a reference type is a reference to an instance of the type.
现在有意义吗?
这篇关于值类型会收集垃圾吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!