I am getting confused with memory allocation basics between Stack vs Heap. As per the standard definition (things which everybody says), all Value Types will get allocated onto a Stack and Reference Types will go into the 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 does the memory allocation will happen in c#? Will the object of MyClass
(that is, m
) will be completely allocated to the Heap? That is, int myInt
and string myString
both will go to heap?
Or, the object will be divided into two parts and will be allocated to both of the memory locations that is, Stack and Heap?
While a stack is used mainly for static memory allocation, a heap is used for dynamic memory allocation. One of the things stack and heap have in common is that they are both stored in a computer's RAM.
Stack and Heap memory are allocated to a program by the Java Virtual Machine (JVM). All the primitive data types and references to objects created inside a method are stored in the stack. Stack memory is accessed in a Last-In-First-Out (LIFO) manner. The size of the stack is small and fixed.
The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or free.
Stack space is mainly used for storing order of method execution and local variables. Stack always stored blocks in LIFO order whereas heap memory used dynamic allocation for allocating and deallocating memory blocks. Memory allocated to the heap lives until one of the following events occurs : Program terminated.
You should consider the question of where objects get allocated as an implementation detail. It does not matter to you exactly where the bits of an object are stored. It may matter whether an 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 having to optimize garbage collection behavior.
While reference types are always allocated on the heap in current implementations, value types may be allocated on the stack -- but aren't necessarily. A value type is only allocated on the stack when it is an unboxed non-escaping local or temporary variable that is not contained within a reference type and not allocated in a register.
Is there anything I missed?
Of course, I would be remiss if I didn't link to Eric Lippert's posts on the topic:
m
is allocated on the heap, and that includes myInt
. The situations where primitive types (and structs) are allocated on the stack is during method invocation, which allocates room for local variables on the stack (because it's 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 must be access via the this
pointer).
"All VALUE Types will get allocated to Stack" is very, very wrong; struct variables can live on the stack, as method variables. However, fields on a type live with that type. If a field's declaring type is a class, the values are on the heap as part of that object. If a field's declaring type is a struct, the fields are part of that struct where-ever that struct lives.
Even method variables can be on the heap, if they are captured (lambda/anon-method), or part of (for example) an iterator block.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With