Wednesday, June 8, 2016

Memory Allocation (Heap and Stack)

Declare a variable, it allocates some memory in the RAM.
This memory has 3 thins: name of the variable, data type of the variable, and the value of the variable.


Stack is keeping track of the running memory needed in your application. (LIFO (Last In First Out) 
=
Value Types hold both data and memory on the same location.(e.g. Enum, Struct, Bool, Int , Bool, Decimal)
int i=4;
int y=i;
(Both variables have different copies)
__________________________________________

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.
=
Reference Type has a pointer which points to the memory location. ( e.g. String Class, Interface, delegate Object)
Class1 obj = new Class1();
Class1 obj1 = obj;
(Both point to the same memory location.)
(pointer on the stack, actual object is at heap.

value type inside object is store in heap.

Boxing - value types to reference types.
UnBoxing reference types to value types.

No comments:

Post a Comment