title: Why don't memory allocators actively return freed memory to the OS?
body: <p>Yes, this might be the third time you see this code, because I asked two other questions about it (<a href="https://stackoverflow.com/q/45529430/8385554">this</a> and <a href="https://stackoverflow.com/q/45537965/8385554">this</a>)..
  The code is fairly simple:</p>
  
  <pre><code>#include &lt;vector&gt;
  int main() {
      std::vector&lt;int&gt; v;
  }
  </code></pre>
  
  <p>Then I build and run it with Valgrind on Linux:</p>
  
  <pre><code>g++ test.cc &amp;&amp; valgrind ./a.out
  ==8511== Memcheck, a memory error detector
  ...
  ==8511== HEAP SUMMARY:
  ==8511==     in use at exit: 72,704 bytes in 1 blocks
  ==8511==   total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
  ==8511==
  ==8511== LEAK SUMMARY:
  ==8511==    definitely lost: 0 bytes in 0 blocks
  ==8511==    indirectly lost: 0 bytes in 0 blocks
  ==8511==      possibly lost: 0 bytes in 0 blocks
  ==8511==    still reachable: 72,704 bytes in 1 blocks
  ==8511==         suppressed: 0 bytes in 0 blocks
  ...
  ==8511== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
  </code></pre>
  
  <p>Here, Valgrind reports there is no memory leak, even though there is 1 alloc and 0 free.</p>
  
  <p>The answer <a href="https://stackoverflow.com/a/45537999/8385554">here</a> points out the allocator used by C++ standard library don't necessarily return the memory back to the OS - it might keep them in an internal cache.</p>
  
  <p>Question is:</p>
  
  <p>1) Why keep them in an internal cache? If it is for speed, <em>how</em> is it faster? Yes, the OS needs to maintain a data structure to keep track of memory allocation, but this the maintainer of this cache also needs to do so.</p>
  
  <p>2) How is this implemented? Because my program <code>a.out</code> terminates already, there is no other process that is maintaining this memory cache - or, is there one? </p>
  
  <p>Edit: for question (2) - Some answers I've seen suggest "C++ runtime", what does it mean? If "C++ runtime" is the C++ library, but the library is just a bunch of machine code sitting on the disk, it is not a running process - the machine code is either linked to my <code>a.out</code> (static library, <code>.a</code>) or is invoked during runtime (shared objects, <code>.so</code>) in the process of <code>a.out</code>.</p>