值类型和引用类型的.NET框架的内存分配 [英] memory allocation of value types and reference types in .net framework

查看:228
本文介绍了值类型和引用类型的.NET框架的内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个先进的一篇文章,我可以读,可以解释内存是如何分配给不同类型的(价值和参考)在.NET框架。

Is there an advanced article which I can read that can explain how memory is allocated for different types (value and reference) in .net framework.

例如,我们知道,值类型在堆栈上分配空间,但究竟是被管理的?

for example we know that value types are allocated space on a stack, but how is that managed?

另外如何在一堆托管引用类型和存储在哪里的实际值。 (引用类型就像任何类将包含许多值类型,他们在哪里保存它们是如何被管理的)

Also how are reference types managed in a heap and where are the actual values stored. (Reference type like any Class will contain many value types, where are they saved and how are they managed)

推荐答案

这比你想象的要复杂得多。即使你声称值类型分配在堆栈上是不正确的。例如:

It's more complicated than you might think. Even your claim that "value types are allocated on the stack" isn't correct. For example:

class Foo
{
    int x;
}

INT 是值类型,但是x的值将始终是在堆上,因为它会被存储的数据的其余部分为富的实例,是一个类

int is a value type, but the value for x will always be on the heap because it will be stored with the rest of the data for the instance of Foo which is a class.

此外,抓获变量匿名函数和迭代器模块使生活棘手。

Additionally, captured variables for anonymous functions and iterator blocks make life trickier.

我有关于C#堆/堆栈内存的文章,你可能会发现有用的,但你可能还需要阅读关于<一个埃里克利珀的博客文章href="http://blogs.msdn.com/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx">"The栈是一个实现细节。特别是,未来的C#编译器可以决定来存储所有的局部变量的堆,使用堆栈仅仅是保持一个参考的方法开始创建一个实例。 ..这不会违抗C#规范的。

I have an article about C# heap/stack memory you may find useful, but you might also want to read Eric Lippert's blog post on "The stack is an implementation detail". In particular, a future C# compiler could decide to store all of its local variables on the heap, using the stack just to hold a reference to an instance created at the start of the method... that wouldn't defy the C# spec at all.

这篇关于值类型和引用类型的.NET框架的内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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