Stack and Heap Memory in .NET

Stack and Heap Memory in .NET

Stack and Heap Memory in .NET

Let’s try to understand what exactly happens when we declare a variable into .NET application.

When we declare a variable into .NET application, it allocates a memory into RAM where it stores

1.????? Name of variable

2.????? Data type of variable

3.????? Value of variable

?

The above image shows what exactly happened in memory. Application allocates memory based upon data type of variable on stack OR heap.

?

Stack and Heap Memory

There are two types of memory allocated into .NET applications, one is stack, and another is heap. Let’s understand by example


When “Statement 1” executed, complier will allocate memory into stack. ?The stack memory is responsible for keeping track of the running memory needed in your application.

?

?

When “Statement 2” is executed, compiler will allocate memory into stack again.


When “Statement 3” is executed, it will allocate memory into heap for class object.


Note:?The reference pointers are allocated on the stack. The statement,?EmployeeDTO employeeDTO,?does not allocate any memory for an instance of?EmployeeDTO.?It only allocates a variable with the name employeeDTO in the stack and sets its value to null. When it hits the new keyword, it allocates memory in the heap.

?

?

Why .NET has 2 types of memories

In C#, primitive data types, such as int, double, bool, etc., hold a single value. On the other hand, the reference data types, or object data types are complex, i.e., an object data type or reference data type can have reference to other objects and other primitive data types.

Because object types hold complex data and further reference of other objects as well, that’s why they are allocated dynamic memory (heap memory), whereas when we declare any variable of primitive data types, complier already know how much memory need to be allocated.

?

How is Heap Memory Freed Up?

The memory allocation on the stack is deallocated when the control moves out from the method, i.e., once the method completes its execution. On the other hand, the memory allocation, which is done on the heap, needs to be de-allocated by the garbage collector.

When an object stored on the heap is no longer used, that means the object does not have any reference pointing. Then, the object is eligible for garbage collection. The garbage collector will de-allocate this object from the heap at some point.

Key Differences Between Stack and Head Memory in .NET:

  • Management:?Stack memory is automatically managed by the system, whereas heap memory is dynamically allocated and deallocated by the garbage collector.
  • Speed:?Stack memory is generally faster than heap memory because of its organization and the way it’s managed.
  • Size:?The stack has size limits based on the thread, but the heap can dynamically grow as needed (limited by the system’s available memory).
  • Access:?Stack memory access is more straightforward and faster, while heap memory requires more complex management.
  • Storage:?Value types are stored in stack memory, while reference types are stored in heap memory.

?

?

?

要查看或添加评论,请登录

Satya Prakash Chhikara的更多文章

  • Difference between RPC and REST

    Difference between RPC and REST

    Difference between RPC and REST Remote Procedure Call (RPC) and REST are two most followed architecture in API design…

  • Checksum

    Checksum

    Checksum Checksum is the outcome of running an algorithm, called a cryptographic hash function on a piece of data…

  • Proxy Server

    Proxy Server

    Proxy Server Definition A proxy server is a router that provides a gateway between users and the internet. It prevents…

  • Lambda Expressions in C#

    Lambda Expressions in C#

    Lambda Expressions in C# Lambda Expressions are shorthand writing for anonymous methods. Let’s look at anonymous method.

  • Anonymous Method with C#

    Anonymous Method with C#

    Anonymous Method As name suggests, anonymous method is a function without having a name. You can say code block with a…

  • Generic Delegates

    Generic Delegates

    Generic Delegate Before discussing generic delegates, it takes an example of delegates. Delegate is a pointer to an…

  • Multicast Delegate

    Multicast Delegate

    Multicast Delegate Multicast Delegate is a delegate which holds references of more than a function pointer having same…

  • Events, Delegates, Events Args and Event Handler in C#

    Events, Delegates, Events Args and Event Handler in C#

    Events, Delegates, Events Args and Event Handler in C# Events, delegates, events args and event handler are quite…

  • Finalize and Dispose in C#

    Finalize and Dispose in C#

    Finalize and Dispose in C# Finalize method 1. This method is used to release resources before the current object is…

  • Choosing between SQL and NoSQL

    Choosing between SQL and NoSQL

    Choosing between SQL and NoSQL When it comes to choosing database between SQL and NoSQL, it’s become tough and…

社区洞察

其他会员也浏览了