title: Track Memory Usage in C++ and evaluate memory consumption
body: <p>I came across the following problem with my code: I was using Valgrind and gperftools to perform heap checking and heap profiling to see if I release all the memory that I allocate. The output of these tools look good and it seems I'm not loosing memory. However, when I'm looking at <code>top</code> and the output of <code>ps</code> I'm confused because this basically does not represent what I'm observing with valgrind and gperftools.</p>
  <p>Here are the numbers:</p>
  <ul>
  <li>Top reports: <strong>RES 150M</strong></li>
  <li>Valgrind (Massif) reports: <strong>23M</strong> peak usage</li>
  <li>gperftools Heap Profiler reports: <strong>22.7M</strong> peak usage</li>
  </ul>
  <p>My question is now, where does the difference come from? I tried as well to track the stack usage in Valgrind but without any success.</p>
  <p>Some more details:</p>
  <ul>
  <li>The process is basically loading data from mysql via the C api to an in-memory storage</li>
  <li>Performing a leak check and breaking shortly after the loading is done, shows a definitive lost of 144 bytes, and 10M reachable, which fits the amount that is currently allocated</li>
  <li>The library performs no complex IPC, it starts a few threads but only one of the threads is executing the work</li>
  <li>It does not load other complex system libraries</li>
  <li>the PSS size from /proc/pid/smaps corresponds to the RES size in TOP and ps</li>
  </ul>
  <p>Do you have any ideas, where this difference in reported memory consumption comes from? How can I validate that my program is behaving correctly? Do you have any ideas how I could further investigate this issue?</p>