Open In App

C# Memory Management

Last Updated : 19 Sep, 2025
Comments
Improve
Suggest changes
1 Likes
Like
Report

In C#, memory management is primarily handled by the Common Language Runtime (CLR). As a developer, you don’t need to manually allocate or release memory, since the runtime manages it for you. However, understanding how memory is organized into stack and heap areas is important for writing efficient and reliable applications.

The memory used by a C# program is divided into two key areas:

memory_allocation
Memory Allocation in C#

Stack Memory

The stack is a simple, fast memory structure used during method execution. It works in a Last In First Out (LIFO) manner.

What Goes on the Stack?

  • Value types such as int, float, bool, char, and struct.
  • Primitive local variables declared inside a method.
  • Method call details including return addresses and passed parameters.
  • References to objects that reside in the heap.

Characteristics

  • Memory allocation and deallocation happen automatically when methods are called and exited.
  • Data stored on the stack has a short lifetime, tied to the method execution.
  • Access to stack data is very fast because it only involves moving a pointer.

Example:

C#
void Calculate() {
    int a = 5;    // stored in stack
    int b = 10;   // stored in stack
    int result = a + b; // stored in stack
}

When Calculate() finishes, all variables (a, b, result) are cleared from the stack.

Heap Memory

The heap is a larger memory area used for objects that need a dynamic lifetime. Unlike the stack, the heap does not follow LIFO ordering.

What Goes on the Heap?

  • Reference type objects such as instances of class, arrays, delegates, and strings.
  • Fields of reference types (e.g., object properties).
  • Data structures whose size and lifetime cannot be determined at compile time.

Characteristics

  • Allocation is dynamic, done at runtime.
  • Objects remain in the heap until they are no longer referenced.
  • Access to heap data is slower than stack because it requires following references.
  • Managed by the Garbage Collector (GC).

Example:

C#
class Student {
    public string Name;
}

void CreateStudent() {
    Student s1 = new Student(); // reference s1 on stack, object on heap
    s1.Name = "Alice";          // field stored in heap
}

Here, the variable s1 (reference) lives on the stack, but the Student object and its Name field live in the heap.

Relationship Between Stack and Heap

Stack and heap are not isolated, they work together

  • A reference variable (on stack) points to an object (on heap).
  • When the reference goes out of scope, the object becomes eligible for garbage collection.

Stack vs Heap

AspectStackHeap
StoresValue types, method call data, object referencesObjects of reference types, their fields, arrays, strings
AllocationAutomatic (LIFO)Dynamic at runtime
LifetimeUntil method endsUntil no references remain
Access SpeedVery fastSlower than stack
SizeLimited, usually smallerLarger, flexible

Article Tags :

Explore