STACK AND HEAP

 



Primitive Data Type : Number, float, Boolean etc. are called Primitive Data Type Most probably , Primitive Data Types created on stack.

STACK AND HEAP

When .NET application runs then all variables allocate in RAM / temp memory and its operating system/programming language divide it into two types of memory one is Stack and another one Heap 
                                                      OR
Stack is used for Static Memory allocation and Heap for Dynamic memory allocation and both are stored in the RAM.

STACK
  • Stack always reserved in the Last in first out i.e. LIFO.
  • Variable allocated on the stack are stored directly to the memory and access to this memory is very fast. 
  • We can use the Stack  if we know exactly how much data have required to allocate before compile time. While in Heap we don't know exactly.
  • Insert the data item at the top of Stack is called PUSH.
  • Delete the data item at the top of Stack is called POP.
HEAP
  • Heap is a area of memory where chunk are allocated to store to certain kind of data object.
  • Variable allocated on the Heap have their memory allocated at run time and accessing this memory bit slower.
C# Code
static void Main(string[] args)
        {
            //line1
            int i10;
            //line2
            int y = i;
            // line3
            Customer x = new Customer();
            x.name = "Raushan";
            // line 4
            Customer z = x;
            z.name = "Kumar"

            Console.WriteLine("Hello World!");
        }

Line 1 : It is a Primitive Data Type and runs and i=10 assigned  into the stack with value and its address pointer. When memory address and value has stored at same place is termed as Value Types i.e.  " Value type are those type which address and actual data stored at same place".

Line 2: When it will run then value of y=10 stored in top of stack and it will never effect the value of i and vice versa is termed as by value Type. Mostly Primitive Data type created on stack.


Line 3: A Class which is not a primitive data type its an object. And in that case its address stored into the stack and value stored into Heap its termed as reference type i.e. "Reference type are those type where in the pointer is stored on the stack but actual data stored on the Heap".

Line 4: we have assign value to z =x it means that reference of z and x is same in case of value type reference of both i & y was different. so when we change the value of x then value of z will be also change and vice-versa its also comes under reference type.

Memory Leak
Memory Leak Occurs when a programmer create a memory in heap and forget to delete it. To avoid this leak memory allocated on heap should always be freed when no longer needed.

Garbage Collector
When class object is created at runtime, certain memory space allocated to it in the Heap memory .After all action of objects are completed in the program there has no longer need of memory allocated space then Garbage Collector automatically release the memory space. 




                             


                                                         Thank You...!

ليست هناك تعليقات:

إرسال تعليق