在.NET栈和堆内存分配 [英] Stack and Heap memory allocation in .net
问题描述
我一直在阅读关于这一主题的不同文章/页,终于来到<一个href="http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx"相对=nofollow>这篇文章,害得我一个困惑!
I've been reading different articles/pages on this topic and finally came to this article, which led me to a confusion!
在文章中,它提到值类型总是在那里他们被宣布
,其中作者的意思,值类型可以驻留在栈或堆,根据如何/在那里他们宣布。
In the article, it's mentioned that Value Types always go where they were declared
, by which the author meant, value types may reside in either the stack or the heap, as per how/where they're declared.
让我放下了code片段来让自己更清楚:
Let me put down a code snippet to make myself more clear:
public class Test
{
int testInt;
string testString;
}
int anInt;
string aString;
Test testObj;
testObj = new Test();
执行的code这几行后,内存分配会是这个样子:
After executing these lines of code, the memory allocation will look something like this:
该结构 testInt
存储在堆栈,因为它是在测试
类中声明。
The struct testInt
is stored in the heap, since it was declared in the Test
class.
保持这个例子在心中,让我们看到后面的地方,我宣布一个整数的简单Form.cs code。
Keeping this example in mind, let us see a simple Form.cs code behind where I'm declaring an integer.
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int anotherInt;
}
}
我的困惑部分:
在这种情况下,如果是 anotherInt
分配?栈或堆?通过它的外观,我想大多数的答案将是堆栈。但是,是不是在一类被称为 Form1中
宣布这个变量?因此,按照第一个code上面的代码中,应该不是将要堆?如果是的话,那么在什么情况下,都将一个结构被分配到堆栈?只有当它是一个方法内声明的?但是尽管如此,也不会一种方法来的类,这再次应存放在一个堆下?
In this case, where is anotherInt
allocated to? Stack or Heap? By the looks of it, I think most of the answers will be "Stack". But, isn't this variable declared in a class called Form1
? So, as per the first code snippet above, shouldn't it be going to the heap? If yes, then under what all circumstances would a struct be allocated to a Stack? Only if it is declared inside a method? But still, won't a method come under a class, which again should be stored in a heap?
有一个很大的问题,我知道了!但只是好奇,想知道发生了什么事。我希望我的问题是清楚的。
A lot of questions I know! But just curious to know what's happening. I hope my question is clear.
推荐答案
在您的例子 anotherInt
将被分配到堆。这是因为 anotherInt
是 Form1中
,这是一个分配的堆对象的字段。堆栈是与线程和放大器相关;只包含引用/当前执行的code对象必要的。因此,要回答你关于方法的类在未来的问题,你是不是entierly正确的存在。
In your example anotherInt
would be allocated to the Heap. This is because anotherInt
is a field of Form1
, which is a Heap allocated object. The Stack is associated with a thread & contains only the references/objects necessary for the currently executing code. So to answer your question about methods coming under a class, you're not entierly correct there.
虽然方法属于一类,他们是code直接与类关联(这是什么 anotherInt
是)可执行块,而不是内存块。其中一个研究这种类型配置的最佳方法是使用诸如WinDbg的&放一个内存调试;实际上审视自己的线程堆栈主场迎战堆。这会给你的,其中一个特定的结构实际分配最清晰的画面。
While methods belong to a class, they are executable blocks of code rather than memory blocks associated directly with a class(which is what anotherInt
is). One of the best ways to examine this type of allocation is to use a memory debugger like WinDbg & actually examine your thread stacks vs. the Heap. This will give you the clearest picture of where a particular struct is actually allocated.
在一个非常简单的道理:堆栈=必需当前正在执行的code堆,堆=一切地址。但ultimatley乔恩B在W /他的链接Eric的博客点。你真的不需要知道你的对象被分配。
In an extremely simplified sense: Stack = addresses necessary for currently executing code stack, Heap = everything else. But ultimatley Jon B is spot on w/ his link to Eric's blog. You really don't need to know where your objects are allocated.
编辑: 包括<一href="http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx"相对=nofollow>博客链接。
这篇关于在.NET栈和堆内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!