Introduction
Memory management is an important part of .net framework. If computer program incorrectly manages memory allocation and object is stored in memory but can't be accessed by the executing or running code then memory leakage happens. If object reference objects are rooted and then can't be garbage collected then in this case memory leakage happens.
Thus, memory management is the important part of any programming language frameworks.
Different kinds of memory being used by .net framework:
- Stack
- Unmanaged Heap
- Managed Heap
Stack
Stack keeps track of everything. It tracks the state of and execution thread and all the methods calls made. Whenever a method is being created .Net creates a container called a Stack Frame. And, keeps track of method parameters, variables, address of the line of code, etc.
Below are the some features of Stack in .net Framework memory:
- It is managed on a per thread basis.
- It is used to store local variables, method parameters, and temporary variables.
- GC doesn't clean the stack as it get automatically cleaned when the method returns.
- The references to objects are stored on the Stack, but the actual objects get allocated on the Heap.
Example:
- void MyMethod()
- {
- MyClass myClassObj =new MyClass();
- Console.WriteLine(myClassObj .Text);
- }

Heap
Heap stores all the reference types based on their as size. They are stored either on SHO or LOH. These are classes, Interface, Delegate, String, etc.
Only object reference is stored on Stack if an instance of reference types is being created. The actual instance is created on the Heap and its address store on the Stack.
Basically .net reserves two sections of Heap for storing objects:
- SOH (Small Object Heap) : It stores the object containing size less than 85K.
- LOH (Large Object Heap) : It stores all larger objects.
Example:
- public class MyTestClass
- {
- strinig MyTest = "This is Test Message";
- byte[] myData = new byte[50000];
- }
So, Heap Allocation will be like below figure for this class:

Unmanaged Heap
- Here, Unmanaged codes are being allocated.
- Managed codes can also allocated this unmanaged heap by calling Win32 APIs.
Managed Heap
- Objects for managed codes are being allocated here. GC takes cares of this memory.
What determines the size of Stack and Heap?
The size of the Stack is set when thread is created. And, size of the Heap is being created or set on Application Startup and can grow if required.
Which is faster either Stack of Heap?
The Stack is faster because its access pattern is very good as it is pointer based. So, its allocation and deallocation is also faster.
Heap has complex booking involved in the allocation & deallocation.
What are the Scopes of Heap and Stack?
The Stack is attached to a Thread. So, when the thread exists the stack is reclaimed.
The Heap is allocated on Application Startup by run time. And, it is reclaimed when application exists.
Conclusion
So, this is the first part of .net memory allocation technique. I will continue this in the next part.

Raymond OpriyePosted Dec 14, 2021, 12:19 PM
Excellent, I am more informed
SubashPosted Sep 23, 2016, 3:18 AM
Yes sir it is easy t understand the basic idea of GC
Abhijit ShettyPosted Feb 27, 2015, 2:27 AM
Nice article but i got a question here... when does Gen1 collection happens1. When GC checks for Gen0; it also checks Gen1 and collects it if full... so that first Gen1 will be collected and then Gen0 2. When GC performs collection on Gen0 and gets survived objects; when these objects need to move to Gen1 and GC finds no space for survived objects then Gen1 collection occurs... so that first Gen0 will be collected and then Gen1
Jasminder SinghPosted Nov 1, 2014, 7:12 AM
Jeetendra Gund very simple and easy to understand...!!! Gud one...!!!
Saineshwar BageriPosted Oct 30, 2014, 1:28 PM
nice one
Nagaraj SPosted Oct 30, 2014, 12:58 PM
Good explanation. Awaiting to see more articles from you. All the best.
Jeetendra GundPosted Oct 30, 2014, 5:19 AM
Thanks, Abhijit Patil sir and @Deepali Gawade.
Abhijit PatilPosted Oct 30, 2014, 1:09 AM
Nice artical Jeetendra sir,keep it up..
Deepali GawadePosted Oct 30, 2014, 12:50 AM
Nice Article.Good Explanation.Keep it up.