Difference between Stack and Heap
Before going into any technical discussion, let’s try to understand normal meaning (not technical language) of these words. Stack is defined as more or less orderly arranged items. You will find example like books in shelves are stack. Heap is defined as a group of things placed, thrown, or lying one on another. Common example is pile of stones lying on ground. Have a look at the images. If you are clear with the literal meaning of these words, then there is no difference in technical meaning also.[linkad]
Let’s talk in some technical terms. In Stack, as per the literal meaning, objects (don’t go in Object Oriented concepts) are stored in memory in an ordered way. If you have read Stack in Data Structure course, same LIFO concept is applied here in allocating and freeing memory. Memory element which is allocated last will be freed first.
Heap, somewhat unordered memory allocation, authors prefer random word over unordered. But somehow if you understand the meaning words do not make a much difference. In heap, memory need not be allocated continuously. Memory is allotted in chunks so you need some more bookkeeping data or metadata. (Again authors like these words but this simply means you need some mechanism to store the data about which all stones in the stone pile make a group. Stones which are supposed to form a group might not be lying together in the pile)[linkad]
If you are reading this question for interview purpose, remember these points
- Stack is used for static memory allocation and Heap for dynamic memory allocation.
- Stack is thread specific and Heap is application specific.
- Memory block in stack will be freed when thread is terminated while heap is freed only after application termination.
Now, you have given these answers in interview and you are selected. You get promoted in your job and you come to architect/designer level. Things get a bit complicated here. Now you have questions like how Stack and Heap are maintained internally. How OS affects or controls these? How your application architecture will use these? There is no single answer to these questions. All these answers are specific to environment and implementation. Some architecture is rigid in nature and they won’t allow you to increase heap size during execution of application while some others might allow you. (Remember heaps are application specific and not thread specific) How much memory per thread should be allotted could be configurable in application and could be be OS specific too. So instead of coming to any general answer, I will suggest you to go through the manual given by the vendor of that particular application architecture and operating system for more granular details.
There are two exceptions which you will generally see while working with Stack and Heap – Stack Overflow and OutOfMemory. I avoided the discussion knowingly because these two problems have different solutions depending upon the application architecture and resources both. I will try to write solutions of these in some future posts with reference to some particular scenarios.