title: Why is memory allocation on heap MUCH slower than on stack?
body: <p>I have been told this many times. But I don't know WHY...What extra cost is involved when allocating memory from heap? Is it hardware related? Is it related to CPU cycles? So many guesses but no exact answers...Could someone give me some elaboration?</p>
  <p>Just as &quot;unwind&quot; said, the Heap data structure is more complicated than Stack. And In my opinion, some memory space is allocated to a thread as its Stack when it starts to run, while the heap is shared by all the threads within a process. This paradigm require some extra mechanism to manage each thread's usage of the shared heap, such as Garbage Collection. Am I right on this?</p>
  <h2>ADD 1 - 10:42 AM 5/13/2022</h2>
  <p>The management of a stack only involves the instruction and registers (SP, BP), which is naturally/purely hardware in a sense.</p>
  <p>While for a heap, it further involves complex software data structures and algorithms, which involves function calling (again stack involved), memory access, etc.</p>
  <p>Hardware is fast but not as flexible as software.</p>
  <p>Software is flexible but not as fast as hardware.</p>
  <p>So heap is not cheap.</p>