.NET Memory Management - Part One

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 executing or running code, then memory leakage happens. If object reference objects are rooted and can't be garbage collected, then in this case memory leakage happens.
 
Thus, memory management is the important part of any programming language framework.
 
Different kinds of memory being used by .NET framework:
  1. Stack
  2. Unmanaged Heap
  3. Managed Heap

Stack

Stack keeps track of everything. It tracks the state of and execution thread and all the method calls made. Whenever a method is being created .NET creates a container called a Stack Frame. And, it keeps track of method parameters, variables, address of the line of code, etc.
 
Below are 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.
Stacks can store variables that are the primitive data types like byte, Int16, structs, double, etc.
 
Example
  1. void MyMethod()  
  2. {  
  3.   MyClass myClassObj =new MyClass();  
  4.   Console.WriteLine(myClassObj .Text);  
  5. }  
Below figure will explain the role of Stack in memory allocation for the above code, 

Heap

Heap stores all the reference types based on their 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 stored on the Stack.
 
Basically .NET reserves two sections of Heap for storing objects,
  1. SOH (Small Object Heap) : It stores the object containing size less than 85K.
  2. LOH (Large Object Heap) : It stores all larger objects.
Example
  1. public class MyTestClass  
  2. {  
  3.    strinig MyTest = "This is Test Message";  
  4.    byte[] myData = new byte[50000];  
  5. }  
Here, MyTestClass has 20 characters and byte size is 50000. So, Heap Allocation will be like below figure for this class,

Unmanaged Heap

  • Here, Unmanaged codes are being allocated.
  • Managed codes can also allocate 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, Stack or Heap?

The Stack is faster because its access pattern is very good as it is pointer-based. So, its allocation and de-allocation is also faster. Heap has complex booking involved in allocation and de-allocation.

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.