title: How to query amount of allocated memory on Linux (and OSX)?
body: <p>While this might look like a duplicate from other questions, let me explain why it's not.</p>
  
  <p>I am looking to get a specific part of my application to degrade gracefully when a certain memory limit has been reached.  I could have used criteria based on remaining available physical memory, but this wouldn't be safe, because the OS could start paging out memory used by my application before reaching the criteria, which would think there is still some physical memory left, and keep allocating, etc.  For the same reason, I can't used the amount of physical memory currently used by the process, because as soon as the OS would start swapping me out, I would keep allocating as the OS pages memory so the number would not grow anymore.</p>
  
  <p>For this reason, I chose a criteria based on the amount of memory allocated by my application, i.e. very close to virtual memory size.</p>
  
  <p>This question (<a href="https://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process">How to determine CPU and memory consumption from inside a process?</a>) provides great ways of querying the amount of virtual memory used by the current process, which I THOUGHT was what I needed.</p>
  
  <p>On Windows, I'm using <code>GetProcessMemoryInfo()</code> and the <code>PrivateUsage</code> field, which works great.</p>
  
  <p>On Linux, I tried several things (listed below) that did not work.  The reason why virtual memory usage does not work for me is because of something that happens with OpenCL context creation on NVidia hardware on Linux.  The driver reserves a region of the virtual memory space big enough to hold all RAM, all swap and all video memory.  My guess is it does so for unified address space and everything.  But it also means that the process reports using enormous amounts of memory.  On my system for instance, <em>top</em> will report 23.3 Gb in the VIRT column (12 Gb of RAM, 6 Gb of swap, 2 Gb of video memory, which gives 20 Gb reserved by the NVidia driver).</p>
  
  <p>On OSX, by using <code>task_info()</code> and the <code>virtual_size</code> field, I also get a bigger than expected number (a few Gb for an app that takes not even close to 1 Gb on Windows), but not as big as Linux.</p>
  
  <p>So here is the big question: how can I get the amount of memory allocated by my application?  I know that this is a somewhat vague question (what does "allocated memory" means?), but I'm flexible:</p>
  
  <ul>
  <li>I would prefer to include the application static data, code section and everything, but I can live without.</li>
  <li>I would prefer to include the memory allocated for stacks, but I can live without.</li>
  <li>I would prefer to include the memory used by shared libraries, but I can live without.</li>
  <li>I don't really care for mmap stuff, I can do with or without at that point.</li>
  <li>Etc.</li>
  </ul>
  
  <p>What is really important is that the number grows with dynamic allocation (new, malloc, anything) and shrinks when the memory is released (which I know can be implementation-dependent).</p>
  
  <h1>Things I have tried</h1>
  
  <p>Here are a couple of solutions I have tried and/or thought of but that would not work for me.</p>
  
  <ol>
  <li><p><strong>Read from /proc/self/status</strong></p>
  
  <p>This is the approach suggested by how-to-determine-cpu-and-memory-consumption-from-inside-a-process.  However, as stated above, this returns the amount of virtual memory, which does not work for me.</p></li>
  <li><p><strong>Read from /proc/self/statm</strong></p>
  
  <p><em>Very slightly</em> worst: according to <a href="http://kernelnewbies.kernelnewbies.narkive.com/iG9xCmwB/proc-pid-statm-doesnt-match-with-status" rel="nofollow noreferrer">http://kernelnewbies.kernelnewbies.narkive.com/iG9xCmwB/proc-pid-statm-doesnt-match-with-status</a>, which refers to Linux kernel code, the only difference between those two values is that the second one does not substract <code>reserved_vm</code> to the amount of virtual memory.  I would have HOPED that <code>reserved_vm</code> would include the memory reserved by the OpenCL driver, but it does not.</p></li>
  <li><p><strong>Use <code>mallinfo()</code> and the <code>uordblks</code> field</strong></p>
  
  <p>This does not seem to include all the allocations (I'm guessing the <code>new</code>s are missing), since for an +2Gb growth in virtual memory space (after doing some memory-heavy work and still holding the memory), I'm only seeing about 0.1Gb growth in the number returned by <code>mallinfo()</code>.</p></li>
  <li><p><strong>Read the [heap] section size from /proc/self/smaps</strong></p>
  
  <p>This value started at around 336,760 Kb and peaked at 1,019,496 Kb for work that grew virtual memory space by +2Gb, and then it never gets down, so I'm not sure I can't really rely on this number...</p></li>
  <li><p><strong>Monitor all memory allocations in my application</strong></p>
  
  <p>Yes, in an ideal world, I would have control over everybody who allocates memory.  However, this is a legacy application, using tons of different allocators, some <code>malloc</code>s, some <code>new</code>s, some OS-specific routines, etc.  There are some plug-ins that could do whatever they want, they could be compiled with a different compiler, etc.  So while this would be great to really control memory, this does not work in my context.</p></li>
  <li><p><strong>Read the virtual memory size before and after the OpenCL context initialization</strong></p>
  
  <p>While this could be a "hacky" way to solve the problem (and I might have to fallback to it), I would really wish for a more reliable way to query memory, because OpenCL context could be initialized somewhere out of my control, and other similar but non-OpenCL specific issues could creep in and I wouldn't know about it.</p></li>
  </ol>
  
  <p>So that's pretty much all I've got.  There is one more thing I have not tried yet, because it only works on OSX, but it is to use the approach described in <a href="https://stackoverflow.com/questions/25609945/why-does-mstats-and-malloc-zone-statistics-not-show-recovered-memory-after-free">Why does mstats and malloc_zone_statistics not show recovered memory after free?</a>, i.e. use <code>malloc_get_all_zones()</code> and <code>malloc_zone_statistics()</code>, but I think this might be the same problem as <code>mallinfo()</code>, i.e. not take all allocations into account.</p>
  
  <p>So, can anyone suggest a way to query memory usage (as vague of a term as this is, see above for precision) of a given process in Linux (and also OSX even if it's a different method)?</p>