title: C++: Measuring memory usage from within the program, Windows and Linux
body: <p>See, I wanted to measure memory usage of my C++ program. From inside the program, without profilers or process viewers, etc.</p>
  
  <p><strong>Why from inside the program?</strong></p>
  
  <ol>
  <li>Measurements will be done thousands of times—must be automated; therefore, having an eye on <code>Task Manager</code>, <code>top</code>, whatever, will not do</li>
  <li>Measurements are to be done <strong>during production runs</strong>—performance degradation, which may be caused by profilers, is not acceptable since the run times are non-negligible already (several hours for large problem instances) </li>
  </ol>
  
  <p><strong>note. Why measure at all?</strong> The only reason to measure used memory (as reported by the OS) as opposed to calculating “expected” usage in advance is the fact that I can not directly, analytically “<code>sizeof</code>” how much does my principal data structure use. The structure itself is </p>
  
  <p><code>unordered_map&lt;bitset, map&lt;uint16_t, int64_t&gt; &gt;</code></p>
  
  <p>these are packed into a <code>vector</code> for all I care (a <code>list</code> would actually suffice as well, I only ever need to access the “neighbouring” structures; without details on memory usage, I can hardly decide which to choose)</p>
  
  <p><code>vector&lt; unordered_map&lt;bitset, map&lt;uint16_t, int64_t&gt; &gt; &gt;</code></p>
  
  <p>so if anybody knows how to “sizeof” the memory occupied by such a structure, that would also solve the issue (though I'd probably have to fork the question or something). </p>
  
  <p><strong>Environment:</strong> It may be assumed that the program runs all alone on the given machine (along with the OS, etc. of course; either a PC or a supercomputer's node); it is certain to be the only one program requiring large (say > 512 MiB) amounts of memory—<em>computational experiment environment</em>. The program is either run on my home PC (16GiB RAM; Windows 7 or Linux Mint 18.1) or the institution supercomputer's node (circa 100GiB RAM, CentOS 7), and the program <strong>may want</strong> to consume all that RAM. <strong>Note</strong> that the supercomputer effectively prohibits disk swapping of user processes, and my home PC has a smallish page file.</p>
  
  <p><strong>Memory usage pattern.</strong> The program can be said to <em>sequentially</em> fill a sort of table, each row wherein is the <code>vector&lt;...&gt;</code> as specified above. Say the prime data structure is called <code>supp</code>. Then, for each integer <code>k</code>, to <em>fill</em> <code>supp[k]</code>, the data from <code>supp[k-1]</code> is required. As <code>supp[k]</code> is filled it is used to <em>initialize</em> <code>supp[k+1]</code>. Thus, at each time, <strong>this</strong>, <strong>prev</strong>, and <strong>next</strong> “table rows” must be readily accessible. After the table is filled, the program does a relatively quick (compared with “initializing” and filling the table), non-exhaustive search in the table, through which a solution is obtained. <strong>Note</strong> that the memory is only  allocated through the <code>STL</code> containers, I never explicitly <code>new()</code> or <code>malloc()</code> myself. </p>
  
  <p><strong>Questions. Wishful thinking.</strong></p>
  
  <ol>
  <li>What is the appropriate way to measure <em>total memory usage</em> (including swapped to disk) of a process <em>from inside its source code</em> (one for Windows, one for Linux)?</li>
  <li>Should probably be another question, or rather a good googling session, but still---what is the proper (or just easy) way to explicitly control (say encourage or discourage) swapping to disk? A pointer to an authoritative book on the subject would be very welcome. Again, forgive my ignorance, I'd like a means to say something on the lines of “NEVER swap <code>supp</code>” or 
  “swap <code>supp[10]</code>”; then, when I need it, “unswap <code>supp[10]</code>”—all from the program's code. I thought I'd have to resolve to serialize the data structures and explicitly store them as a binary file, then reverse the transformation.</li>
  </ol>
  
  <hr>
  
  <p>On Linux, it appeared the easiest to just catch the heap pointers through sbrk(0), cast them as 64-bit unsigned integers, and compute the difference after the memory gets allocated, and this approach produced plausible results (did not do more rigorous tests yet).</p>
  
  <p><strong>edit 5.</strong> Removed reference to <code>HeapAlloc</code> wrangling—irrelevant.</p>
  
  <p><strong>edit 4. Windows solution</strong>
  This bit of code reports the working set that matches the one in Task Manager; that's about all I wanted—tested on Windows 10 x64 (tested by  allocations like <code>new uint8_t[1024*1024]</code>, or rather, <code>new uint8_t[1ULL &lt;&lt; howMuch]</code>, not in my “production” yet ). 
  On Linux, I'd try <code>getrusage</code> or something to get the equivalent.
  The principal element is <code>GetProcessMemoryInfo</code>, as suggested by @IInspectable and @conio</p>
  
  <pre><code>#include&lt;Windows.h&gt;
  #include&lt;Psapi.h&gt;
  //get the handle to this process
  auto myHandle = GetCurrentProcess();
  //to fill in the process' memory usage details
  PROCESS_MEMORY_COUNTERS pmc;
  //return the usage (bytes), if I may
  if (GetProcessMemoryInfo(myHandle, &amp;pmc, sizeof(pmc)))
      return(pmc.WorkingSetSize);
  else
      return 0;
  </code></pre>
  
  <p><strong>edit 5.</strong> Removed reference to <code>GetProcessWorkingSetSize</code> as irrelevant. Thanks @conio.</p>