Memory allocation: stack versus heap?

I am confused with the basics of memory allocation between Stack vs Heap . According to the standard definition (everything everyone says), all Value Types will be distributed by Stack and Reference . Types will go into Heap .

Now consider the following example:

class MyClass { int myInt = 0; string myString = "Something"; } class Program { static void Main(string[] args) { MyClass m = new MyClass(); } } 

Now, how will memory allocation happen in C #? Will the MyClass object (i.e. m ) be completely allocated for the heap? That is, int myInt and string myString will go to the heap?

Or will the object be divided into two parts and allocated to both memory cells, which are a stack and a heap?

+61
stack heap c #
Dec 20 '10 at 6:02
source share
6 answers

m is allocated on the heap and turns on myInt . Situations where primitive types (and structures) are allocated on the stack is a method call that allocates space for local variables on the stack (because it is faster). For example:

 class MyClass { int myInt = 0; string myString = "Something"; void Foo(int x, int y) { int rv = x + y + myInt; myInt = 2^rv; } } 

rv , x , y will all be on the stack. myInt is somewhere on the heap (and there must be access via the this pointer).

+39
Dec 20 '10 at 6:08
source share

You should consider where the objects will be allocated as part of the implementation. It doesn't matter to you where the bits of the object are stored. You may wonder if the object is a reference type or a value type, but you don’t have to worry about where it will be stored until you start optimizing the behavior of the garbage collection.

Although reference types are always allocated on the heap in current implementations, value types can be allocated on the stack, but not necessarily. A value type is allocated only on the stack if it is an unallocated unescapable local or temporary variable that is not contained in the reference type and is not allocated in the register.

  • If the value type is part of the class (as in your example), it will fall into the heap.
  • If it is placed in a box, it will fall into a heap.
  • If it is in an array, it will fall into the heap.
  • If it is a static variable, it will end up on the heap.
  • If it is closed by closure, it will end up on the heap.
  • If it is used in an iterator or asynchronous block, it will fall into the heap.
  • If it is created by unsafe or unmanaged code, it can be allocated to any type of data structure (not necessarily to the stack or heap).

Is there something I missed?

Of course, I would be left out if it weren’t for a link to Eric Lippert's related posts:

+45
Dec 20 2018-10-12T00:
source share

"All VALUE types will be allocated to Stack," this is very, very wrong; structure variables can live on the stack, like method variables. However, fields with a type live with this type. If the field declaration type is a class, the values ​​are on the heap as part of this object. If the field declaration type is a structure, the fields are part of that structure, wherever it exists.

Even valid method variables can be on the heap if they are captured (lambda / anon method) or part (for example) of an iterator block.

+20
Dec 20 2018-10-12T00:
source share

simple measures

The type of value can be labeled STACK; this is an implementation detail that it can highlight for some futurist data structure.

therefore it is better to understand how the value and the reference type work, the value type will be copied by value, which means that when you pass the value type as a parameter to FUNCTION, than it will be copied by nature, then you will have a common new copy.

Link types are passed by reference (againg does not consider the link to store the address again in some future versions, it may be stored in some other data structures.)

therefore in your case

myInt is an int that is initialized in a class that references a reference type, so it will be bound to an instance of the class that will be stored in "HEAP".

I would suggest you can start reading blogs written by ERIC LIPPERTS.

Eric's blog

+1
Dec 20 2018-10-12T00:
source share

Each time an object is created in it, it goes into a memory area known as a heap. Primitive variables of type int and double are allocated on the stack if they are local variables of the method and on the heap if they are members of the variables. In methods, local variables are pushed onto the stack when the method is called, and the stack pointer decreases when the method is called. In a multi-threaded application, each thread will have its own stack, but will share the same heap. This is why you should be careful in your code to avoid concurrent access problems in heap space. The stack is thread safe (each thread will have its own stack), but the heap is not thread safe unless it is protected by synchronization through your code.

This link is also useful http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/

+1
Apr 6 '15 at 18:34
source share



All Articles