When we declare a variable in our .net application. Have we
noticed where it goes and how it stores the value into it and the actual
process what goes inside that?
Our declared variable allocates a chunk of memory into RAM.
This memory has following things into it
Name : variable name (myVar)
Data Type : variable
data type (string)
Value : variable assigned value (‘ved’)
This above given process is happening into RAM (memory). It
depends on our variable declaration of data type. There are two types of memory
allocation while variable stores into RAM (memory).
Stack:
int i = 1;
When above line of code executed the compiler allocates a
small chunk of memory into memory that type of memory stored is called Stack.
Stack is only track the running memory in our application.
In stack execution takes place as variable declaration move
one by one. Stack is as a series of compartment or boxes put on top of one by
one.
Memory allocation and de - allocation is done as LIFO (Last
in First Out) based logic.
Heap:
myClass myObj = new myClass();
The above line creates an object of the class myClass. When
the above line executes it creates a pointer on stack and the actual object
stored in type of different memory which is called ‘Heap’. Unlike Stack heap
does not track running memory. Head is used for dynamic memory.
Value Types and Reference Type:
The type which holds data and memory in the same memory
location is known as Value Type.
Reference Type has a pointer which not allocates the same
memory location but point the memory location where the variable stored.
Strings and Objects are Reference Types Data.
And other .Net primitive data types are Value Type
Comments