Loading, please wait...

A to Z Full Forms and Acronyms

Memory Management in Dot net Framework

Oct 03, 2019 stack heap, 5244 Views
Stack and Heap places the .NET framework stores items in memory as our code executes

Stack and Heap places the .NET framework stores items in memory as your code executes

The stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM.

Stack

Heap Memory

bool,byte,char,decimal,double,enum,float

int,long,sbyte,short,struct

class,interface,delegate,object,string

Variables allocated on the stack are stored directly to the memory

 

 

Variables allocated on the heap have their memory allocated at run time

 

both memory & data on the same location

Not the same

The stack is thread-specific

Heap is application-specific.

stack track the running memory

Heap doesn’t track the running memory

Value Types are also sometimes placed on the Heap.

Stack:

public int Mystackmethod() 
{ 
      MyInt x ;   
 // it only allocates a stack variable MyInt (and sets it to null).
     x = new MyInt(); 
      x.Value = 3;
      return x.Value; 
}

know exactly how much data before compile time.

Heap Memory:

public class Myclassheap
{
       public int Value;
 //value type (no longer accessed, its memory deallocated)
}

 

don't know exactly how much data you will need at runtime , keep in heap.

The stack is always reserved in a LIFO order.  When the top box in the stack is no longer used, it's thrown out (clean up).

Similarly for Heap is clean by Garbage Collection. Simply find all objects in the Heap that is not being accessed by the main program and delete them and allocates memory to other objects.

A to Z Full Forms and Acronyms