Stack and heap memory allocation in .net

I read various articles / pages on this topic and finally came to this article , which led me to confusion!

In this article, he mentioned that Value Types always go where they were declared , by which the author had in mind, the types of values ​​can be either on the stack or on the heap, according to how / where they are declared.

Let me write a code snippet to make myself clearer:

 public class Test { int testInt; string testString; } int anInt; string aString; Test testObj; testObj = new Test(); 

After these lines of code are executed, the memory allocation will look something like this:

enter image description here

The testInt structure is stored on the heap since it was declared in the Test class.

With this example in mind, let's look at the simple Form.cs code where I declare an integer.

 using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public int anotherInt; } } 

My confusion:

In this case, where is anotherInt allocated? Stack or heap? In appearance, I think most of the answers will be "Stack". But isn't this variable declared in a class called Form1 ? So, according to the first code snippet above, shouldn't you be going in a bunch? If so, under what circumstances will the structure be allocated to the stack? Only if it is declared inside a method? But still, would there be a method under the class that should again be stored on the heap?

A lot of questions that I know! But just curious to find out what is happening. Hope my question is clear.

+4
source share
2 answers

In your example, anotherInt will be allocated on the heap. This is because anotherInt is a Form1 field, which is an object allocated by the heap. Stack is associated with the stream and contains only the links / objects needed for the current executable code. Therefore, to answer your question about methods that fall under the class, you are wrong here.

While methods belong to a class, they are executable blocks of code, not memory blocks associated directly with the class (which is anotherInt ). One of the best ways to learn about this type of allocation is to use a memory debugger such as WinDbg, and actually study thread stacks against the heap. This will give you the clearest picture of where a particular structure is actually distributed.

In an extremely simplified sense: Stack = addresses needed to execute the current code stack, Heap = everything else. But ultimatum John B is in place with his link to Eric's blog. You really don't need to know where your objects stand out.

EDIT: Including link.

+2
source

In your example, anotherInt will be located on the heap along with the rest of the Form1 instance (assuming you create it, of course).

To create a local int, a variable must be declared by a method or property. For instance.

 void Foo() { int localInt = 42; } 

Now, just because a value type is declared in a method does not necessarily mean that it is being pushed onto the stack. For instance. captured variables are handled differently.

+2
source

All Articles