title: Is there any way to force the WorkingSet of a process to be 1GB in C++?
body: <p>If I use the following call in C++, I would expect the WorkingSet of the process to never drop below 100MB. </p>
  
  <p>However, the OS still trims the working set back to 16MB, even if I make this call.</p>
  
  <p>Setting WorkingSet to 100MB would lead to a dramatic increase in my application speed, by eliminating soft page page faults (see the diagram below).</p>
  
  <p>What am I doing wrong?</p>
  
  <pre><code>SIZE_T workingSetSizeMB = 100;
  int errorCode = SetProcessWorkingSetSizeEx(
      GetCurrentProcess(),
      (workingSetSizeMB - 1) * 1024 * 1024), // dwMinimumWorkingSetSize
      workingSetSizeMB * 1024 * 1024,  // dwMaximumWorkingSetSize,
      QUOTA_LIMITS_HARDWS_MIN_ENABLE | QUOTA_LIMITS_HARDWS_MAX_DISABLE
    );
  // errorCode returns 1, so the call worked.
  </code></pre>
  
  <p><em>(extra for experts)</em> <strong>Experimental Methodology</strong></p>
  
  <p>I wrote a test C++ project to allocate 100MB of data to bring the WorkingSet over 100MB (as viewed within Process Explorer), then deallocated that memory. However, the OS trimed the WorkingSet back to 16MB as soon as I deallocated that memory. I can provide the test C++ project I used if you wish.</p>
  
  <p>Why is Windows providing a call to SetProcessWorkingSetSizeEx() if it doesn't appear to work? I must be doing something wrong.</p>
  
  <p>The diagram below shows the dramatic increase in the number of soft page faults (the red spikes) when the green line (the working set) dropped from 50MB to 30MB.</p>
  
  <p><img src="https://i.stack.imgur.com/AEyEh.png" alt="Example showing the increase in soft page faults when the WorkingSet is reduced too low"></p>
  
  <p><strong>Update</strong></p>
  
  <p>In the end, we ended up ignoring the problem, as it didn't impact performance that much.</p>
  
  <p>More importantly, SetProcessWorkingSetSizeEx does <em>not</em> control the current WorkingSet, and is <em>not</em> related in any way to soft page faults. All it does is prevent hard page faults, by preventing the current WorkingSet being paged out to the hard drive.</p>
  
  <p>In other words, if one wants to reduce soft page faults, SetProcessWorkingSetSizeEx has absolutely no effect, as it refers to hard page faults.</p>
  
  <p>There is a great writeup in "Windows via C/C++" (Richter) which how Windows deals with memory.</p>