C# Fundamental: Stack vs. Heap
Memory Allocation (Stack and Heap) Operating system and language runtimes such as .NET CLR divides the memory into two chunks called stack ad heap. Stack and heap act differently while allocating memory. When we call a method, the memory required for its parameters ad variable is accommodated by the stack. When a method finishes its job, the memory is automatically released by the garbage collector for the purpose of reuse. Stack Memory Stack is organized like stack of boxes sitting on top of one another. When a method is called, each parameter is put into a box, first box goes first into stack then second on top of it, and so on. As in the following diagram: Stack uses Last In First Out (LIFO) mechanism for allocating memory. Heap Memory Heap memory is an unorganized pile of boxes sitting in a room randomly, not like stack which stored boxes on top of each other in an order. The reference to the object is stored on stack but the actual value is stored on heap....