Monday, September 17, 2018

Stack & Heap Memory

Stack & Heap Memory:

           When we declare a variable in a .NET application, it allocates some space of memory in the RAM. This memory has three things: the name of the variable, the data type of the variable, and the value of the variable.
That was a simple explanation of what happens in the memory, but depending on the data type, your variable is allocated that type of memory. The Common Language Runtime (CLR) automatically handles Memory Management with the help of its components. The Garbage Collector plays an important role in Memory Management. Objects are automatically freed when they are no longer needed by the application by Garbage Collector.

There are two types of memory allocation: stack memory and heap memory.

Stack Memory:

            The stack is used for static memory allocation. It is an array of memory. Think of the stack as a series of boxes stacked one on top of the next. It is a Last-in, First-out (LIFO) data structure. Data can be added to and deleted only from the top of the stack. Placing a data item at the top of the stack is called pushing the item onto the stack. Deleting an item from the top of the stack is called popping the item from the stack. Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it's allocation is done when the program is compiled. The stack is responsible for keeping track of the running memory needed in your application. The stack contains value type data. Mostly stack contains local variables which visible only to owner thread & gets cleared once they lost the scope.

Heap Memory:

The Heap is used for dynamic memory allocation. Unlike the stack, data can be stored and removed from the heap in any order. Your program can store items in the heap, it cannot explicitly delete them.  Variables allocated on the heap have their memory allocated at run time and accessing this memory is a bit slower, but the heap size is only limited by the size of virtual memory. The heap contains reference types. The heap memory is used by all part of application thus objects created on heap visible to all threads. Allocation & de allocation of heap memory is done by Garbage Collector.

For Example:

public void Method1()
{
    // Line 1
    int i=6;

    // Line 2
    int y=8;

    //Line 3
    class1 cls1 = new class1();
}

It’s a three line code, let’s understand line by line:

Line 1:  When line 1 is executed, the compiler allocates a small amount of memory in the stack. The stack is responsible for keeping track of the running memory needed in your application.

Line 2: At line 2 memory allocates on top of the first memory allocation. You can think about stack as a series of compartments or boxes put on top of each other. Memory is allocated and de-allocated at only one end of the memory, i.e., top of the stack.

Line 3: In line 3, we have created an object. When this line is executed it creates a pointer on the stack and the actual object is stored in a different type of memory location called ‘Heap’. ‘Heap’ does not track running memory, it’s just a pile of objects which can be reached at any moment of time. Heap is used for dynamic memory allocation.

Difference between Stack and Heap Memory:


No comments:

Post a Comment