Stack & Heap (Storage of Value Types and Reference Types)

As i have informed in my previous article, In an IT Services compnay interviewer has asked me about the Difference between value types  and reference types in .NET. I have conveyed that Value Types are stored in “Stack” (Datastructure for Memory Management Technique) and Reference Types are stored in “Heap”.

He immediately asked me why we have 2 data structures for handling this, one is enough right. I was sure about Stack, was in a 50-50% situation with explanation about “Heap”. But i believe what i said was right. Before going further we should know the basics right.

So i am quoting a simple article i found in C# Online.NET here

The STACK

The stack is a data structure in memory used for storing items in a last-in, first-out manner (LIFO). The stack contains both local variables and the call stack. In C#, value types are stored on the stack.

Local variables

Local variables are variables that are declared inside a method. They can be of either value type or reference type. Value type variables are stored directly on the stack. A reference type variable is actually a numerical address pointing to a location on the heap where the object is stored.

Call stack

Every C# application begins with a Main method. In turn, it calls other methods which call still other methods. When a method is called, it is added to the top of the stack. And, when a method returns to its caller, it is removed from the stack. Execution continues with the calling method.

Scope

Scope defines the lifetime of a variable. The block of code in which a variable is declared is its scope. When the block in which a variable is declared begins execution, the variable’s lifetime begins when it is pushed onto the stack as part of the call stack. When execution leaves the block in which the variable was declared, the call stack and variables are popped off the stack. The variable’s lifetime has ended.

The HEAP

The heap or managed heap is a data structure in memory where all objects—reference types—are stored. When an object is instantiated, the object is stored on the heap as a block of data containing its data members. Then, the memory address of the block is stored in a reference variable. Future references to the object are through the reference variable.

In C#, the new operator is used to instantiate a class and return the reference to the object on the heap.

The following table summarizes the differences between the Heap and the Stack:

Memory Contents Item Order Item Lifetime Item Removal Timing
Stack value types, stack frames sequential (LIFO) scope pop deterministic
Heap objects random reference count Garbage Collection nondeterministic

 

Courtesy and Thanks to CShap-Online.NET for the article Stack vs Heap

Conclusion..

So the main reason we are having two data-structures for maintaining memory for objects and types.

Simple reason is Reference Types are cleared from HEAP by Garbage Collection and it is been happened in non-deterministic way. Value Types objects are automatically popped out of “STACK” in a deterministic way, that means we knows when it will be cleared from STACK. In Reference Types case we are not sure when it will be cleared or collected for freeing up memory. Since We have garbage collection mechanism to handle the memory management for REFERENCE TYPES, .NET Framework uses two separate data structures. So that management of the objects are handled in a better way.

I welcome your ideas and corrections on my concepts so that i can gain more knowledge on this.