memory – Heap and stack view


What does the heap and stack physically look like in RAM?


  1. Both the stack and the heap are physically in RAM (we do not consider architectural dislocations using special processors / computers)
  2. Their size and location are determined by the axis
  3. In this case, the heap can be fragmented (sometimes quite strongly). Axes usually have special routines for defragmenting the heap.
  4. The stack is usually never fragmented (you can probably think of fragmented stack implementations, but that's an oxymoron).
  5. The stack is, as it were, faster because the only parameter it works with is the stack pointer (usually a register) – therefore, all operations with the stack work many times faster than with the heap. The POP/PUSH write operation from the stack is 1 body movement of the POP/PUSH processor
  6. It is more difficult with a heap precisely because of its fragmentation, and a simple operation of extracting a value from it can result in dozens (if not hundreds) of processor movements.
  7. The disadvantages of the stack are that it is small in size (it is always an order of magnitude smaller than the heap) – and also that access to it is only sequential.
Scroll to Top