不要值类型被垃圾收集的?垃圾、类型

2023-09-02 10:35:47 作者:此男丶有点拽

我知道,引用类型会被垃圾收集。我想知道是否值类型也将垃圾从堆栈中收集的?

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".

该堆栈的 pre-分配的内存块一百万尺寸字节。有时我们使用的内存块的部分来存储的值类型局部变量。什么precisely你说的从堆栈中移除是什么意思?堆栈永远不会改变的大小;这是pre-分配内存的一百万字节的块。

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寄存器指向这些区域之间的边界。你问在什么条件下使用的堆栈值类型特定的局部变量相关联的内存成为无效部分的基础上的ESP在x86架构上登记的价值变化的一部分吗?

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.

延伸阅读:

http://blogs.msdn.com/ericlippert/archive/2009/02/17/references-are-not-addresses.aspx

http://blogs.msdn.com/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx

http://blogs.msdn.com/ericlippert/archive/2009/05/04/the-stack-is-an-implementation-detail-part-two.aspx

http://blogs.msdn.com/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx

我有点糊涂了,现在读你所提到的有关价值和值类型。我发现很难理解上的差异。

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.

一个变量是一个的存储地点的。

每一个变量都有一个的键入的。 A型可以是值类型的或引用类型的。

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.

引用类型变量的值是一个引用类型或空的对象。例如,字符串是引用类型。 string类型的变量的值是一个引用字符串或空。

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.

这是否有道理呢?