简体   繁体   English

类中声明变量的内存分配

[英]Memory Allocation for Variable Declared in Class

As Value type variable allocates memory in Stack where as Reference Type allocates it in Heap.值类型变量在堆栈中分配内存,而引用类型在堆中分配内存。

So how the memory allocated when a value type variable(eg int i =4;) is declared in the reference type (eg. in a class).那么在引用类型(例如在类中)中声明值类型变量(例如 int i =4;)时如何分配内存。

How the overall memory allocation works in .net for value type & reference typ, and also value type inside the scope of refence type.整体内存分配如何在 .net 中用于值类型和引用类型,以及引用类型范围内的值类型。

Please explain it or provide any links regarding that.请解释或提供任何相关链接。

Thanks谢谢

A value type variable allocates memory on the stack whereas a reference type allocates it in heap.值类型变量在堆栈上分配内存,而引用类型在堆中分配内存。

No, that statement is completely wrong.不,这种说法是完全错误的。 Lots of people believe that, but it is obviously false, as you have discovered.很多人相信这一点,但正如您所发现的,这显然是错误的。

How is the memory allocated when a value type variable int i = 4;值类型变量int i = 4;时内存是如何分配的int i = 4; is declared as a field of a reference type?被声明为引用类型的字段?

Clearly you know why your first statement is completely wrong.很明显你知道为什么你的第一个陈述是完全错误的。 The integer field of the class cannot be allocated on the stack because the object might live longer than the stack frame.无法在堆栈上分配类的整数字段,因为该对象的存活时间可能比堆栈帧长。

To understand what is really going on, first you have to realize that there are three kinds of things:要了解真正发生的事情,首先您必须意识到存在三种情况:

  • value types值类型
  • references参考
  • instances of reference type引用类型的实例

References and instances of reference type are completely different, just as a piece of paper containing my address and my actual house are completely different.引用引用类型的实例是完全不同的,就像一张纸上写着我的地址和我的实际房子是完全不同的。

The next thing you have to understand is that there are two kinds of storage: long-term and temporary storage.接下来您需要了解的是,存储有两种:长期存储和临时存储。 Long-term storage is usually called "the heap", but I prefer to think of it simply as long-term storage.长期存储通常称为“堆”,但我更愿意将其简单地视为长期存储。 Temporary storage is usually called "the stack" but that is also misleading because of course there could be multiple stacks, there could be temporaries stored in registers, and so on.临时存储通常被称为“堆栈”,但这也具有误导性,因为当然可能有多个堆栈,可能有临时存储在寄存器中,等等。

An instance of a reference type occupies memory in the long-term storage.引用类型实例在长期存储中占用内存。 (Sometimes it would be possible to determine that an instance of a reference type is short-lived, and put it in temporary storage, but we do not do this optimization in practice.) (有时可以确定引用类型的实例是短暂的,并将其放在临时存储中,但我们在实践中不做这种优化。)

A variable is a storage location which stores either a value of value type or a reference .变量是存储值类型引用的存储位置。

Where the storage location of the variable is allocated depends on the lifetime of the variable .变量的存储位置分配在哪里取决于变量的生命周期 If the variable is a local variable known to be of short lifetime, it is allocated from the temporary storage pool.如果变量是已知生命周期较短的局部变量,则从临时存储池中分配。 If the variable is known to be of long lifetime (because, say, it is an outer variable of a closure) then it is allocated off the long-term storage pool.如果已知该变量具有长生命周期(例如,因为它是闭包的外部变量),则将其从长期存储池中分配。

If the variable is a field of a class, we already know that its storage comes from the long-term pool.如果变量是一个类的字段,我们已经知道它的存储来自长期池。 If the variable is a field of a value type, that value type inhabits storage somewhere;如果变量是值类型的字段,则该值类型驻留在某处的存储中; the field inhabits the same storage.该字段位于相同的存储中。

If the variable is an array element, it is allocated off the long-term storage pool;如果变量是数组元素,则从长期存储池中分配; arrays are instances of reference type.数组是引用类型的实例。

The key to getting your understanding correct is to simply stop believing the myth that whether a variable is of reference or value type affects where the storage is allocated .正确理解的关键是不要再相信变量是引用类型还是值类型会影响存储分配位置的神话。 That is not true and has never been true, and doesn't even make any sense.那不是真的,从来都不是真的,甚至没有任何意义。

The only thing that affects where a variable is stored is how long does the variable live for .唯一影响变量存储位置的是变量存在多长时间 Short-lived variables are allocated off the temporary pool -- the stack, or registers -- and long-lived variables are allocated off the long-term storage pool -- the heap.短期变量从临时池(堆栈或寄存器)分配,长期变量从长期存储池(堆)分配。

This is why Eric Lippert reminds us that the stack is an implementation detail.这就是为什么 Eric Lippert 提醒我们堆栈是一个实现细节。

When an instance of a value type is a member of a reference type yes, it is stored on the managed heap along with the parent object.当值类型的实例是引用类型的成员时,是的,它与父对象一起存储在托管堆中。 It's a good question and something you should understand, just not something that should drive your design in most scenarios.这是一个很好的问题,你应该理解,但不是在大多数情况下驱动你的设计的东西。

structs should be small, simple data types that are relatively cheap to create and pass around. structs应该是小而简单的数据类型,创建和传递的成本相对较低。 Reference types are your complex types, require only a copy of the reference to pass to a method, but of course come with some baggage due to being allocated on the heap.引用类型是您的复杂类型,只需要将引用的副本传递给方法,但由于在堆上分配,当然会带来一些负担。 Here is a good follow up post regarding the implications of stack versus heap allocations.这是关于堆栈与堆分配的影响的很好的后续帖子

There are plenty of references out there which explain the performance implications of value types versus reference types.有很多参考资料解释了值类型与引用类型的性能影响。 You should learn all about it and also understand that, most of the time, it is a semantic decision, not a performance decision.您应该了解所有相关信息,并了解大多数情况下,这是一个语义决策,而不是性能决策。

So how the memory allocated when a value type variable(eg int i =4;) is declared in the reference type (eg. in a class).那么在引用类型(例如在类中)中声明值类型变量(例如 int i =4;)时如何分配内存。

If the object lies on heap, it means all it's member variable(s) lies there.如果对象位于堆上,则意味着它的所有成员变量都在那里。

here is a nice article .这是一篇不错的文章

BTW: not allways goes a Value on the stack - it might end in Heap.顺便说一句:并非总是在堆栈上有一个值 - 它可能在堆中结束。

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

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