|
Answer» Stack - Stack is an array of memory.
- 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.
Heap The heap is an area of memory where chunks are allocated to store certain kinds of data objects. UNLIKE the stack, data can be STORED and removed from the heap in any order. CLR’s garbage collector (GC) automatically cleans up orphaned heap objects when it determines that your CODE can no longer access them. Difference between Stack and Heap Memory | Stack Memory | Heap Memory |
|---|
| Static memory allocation | Dynamic memory allocation | | Variables allocated on stack are directly stored to memory | Variables allocated on Heap have their memory allocated at runtime | | Variables cannot be re-sized | Variables can be re-sized | | Very fast access | Relatively slow access | | Variables stored on stack are visible only to the owner thread | Objects created on the heap are visible to all threads. | | Stack is ALWAYS reserved in LIFO order, the most recently reserved block is always the next block to be freed. | You can allocate a block and free it at any time. | | The moment stack space is exhausted, .NET runtime throws StackOverflowException.Memory | .NET runtime creates special thread that monitors allocation of heap space called Garbage Collector |
|