title: Recycle Freed Objects
body: <p>suppose I need to allocate and delete object on heap frequently (of arbitrary size), is there any performance benefit if instead of deleting those objects, I will return it back to some "pool" to be reused later?</p>
  
  <p>would it give benefit by reduce heap allocation/deallocation?, or it will be slower compared to memory allocator performance, since the "pool" need to manage a dynamic collection of pointers.</p>
  
  <p>my use case: suppose I create a queue container based on linked list, and each node of that list are allocated on the heap, so every call to push() and pop() will allocate and deallocate that node:</p>
  
  <p>`</p>
  
  <pre><code>template &lt;typename T&gt; struct QueueNode {
      QueueNode&lt;T&gt;* next;
      T object;
  }
  
  template &lt;typename T&gt; class Queue {
      void push(T object) {
          QueueNode&lt;T&gt;* newNode = QueueNodePool&lt;T&gt;::get(); //get recycled node
          if(!newNode) {
              newNode = new QueueNode&lt;T&gt;(object);
          }
          // push newNode routine here..
      }
      T pop() {
          //pop routine here...
          QueueNodePool&lt;T&gt;::store(unusedNode); //recycle node
          return unusedNode-&gt;object;
      }
  }
  </code></pre>
  
  <p>`</p>