title: Is there a way to expand the current WorkingSet of a process to 1GB?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/12228321/is-there-any-way-to-force-the-workingset-of-a-process-to-be-1gb-in-c">Is there any way to force the WorkingSet of a process to be 1GB in C++?</a>  </p>
  </blockquote>
  
  
  
  <p>We would like to increase the WorkingSet of a .NET process to 1GB, in advance, to avoid page faults.</p>
  
  <p>Is there a way to do this in .NET?</p>
  
  <p><strong>Update</strong></p>
  
  <p>Unfortunately, it appears that even if we do a call to <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms686237%28v=vs.85%29.aspx" rel="nofollow noreferrer">SetProcessWorkingSetSizeEx</a>, the garbage collection trims the working set down anyway, bypassing MinWorkingSet (see "Automatic GC.Collect() in the diagram below).</p>
  
  <p>In the picture below, is there a way to lock the process WorkingSet (the green line) to 1GB, to avoid the spike in page faults (the red lines) that occur when allocating new memory into the process?</p>
  
  <p>The reason this would be awesome is that every time a page fault occurs, it blocks the thread for 250us, which hits application performance badly.</p>
  
  <p><img src="https://i.stack.imgur.com/Hzldv.png" alt="enter image description here"></p>
  
  <p><strong>Update</strong></p>
  
  <p>Quote from: "Windows via C/C++, Fifth Edition, Jeffrey Richter (Wintellect)"</p>
  
  <blockquote>
    <p>Calls to SetProcessWorkingSetSize by an individual process are ignored
    unless the process is just trying to empty its working set. To set
    this limit, specify the JOB_OBJECT_LIMIT_WORKINGSET flag in the
    LimitFlags member.</p>
  </blockquote>
  
  <p>This book is implying that the only way to set the WorkingSet is by assigning the process to a Job Object and setting JOB_OBJECT_LIMIT_WORKINGSET and MinimumWorkingSetSize.</p>
  
  <p><strong>Update</strong></p>
  
  <p>SetProcessWorkingSetSizeEx has absolutely nothing to do with soft page faults. It only refers to hard page faults, as it prevents memory in the current WorkingSet being paged out to the hard drive.</p>
  
  <p><strong>Update</strong></p>
  
  <p>It turns out that the only method to increase the WorkingSet is to run .NET using an extremely specialized <a href="https://stackoverflow.com/questions/2348207/what-is-clr-hosting">CLR Host</a> written in C++ (see my answer below).</p>