How to set MinWorkingSet and MaxWorkingSet in a 64-bit .NET process?

Posted by Gravitas on Stack Overflow See other posts from Stack Overflow or by Gravitas
Published on 2012-08-30T19:20:25Z Indexed on 2012/08/31 21:38 UTC
Read the original article Hit count: 2186

Filed under:
|
|

How do I set MinWorkingSet and MaxWorking set for a 64-bit .NET process?

p.s. I can set the MinWorkingSet and MaxWorking set for a 32-bit process, as follows:

[DllImport("KERNEL32.DLL", EntryPoint = "SetProcessWorkingSetSize", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
internal static extern bool SetProcessWorkingSetSize(IntPtr pProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize);

[DllImport("KERNEL32.DLL", EntryPoint = "GetCurrentProcess", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
internal static extern IntPtr MyGetCurrentProcess();

// In main():
SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, int.MaxValue, int.MaxValue);

Update:

Unfortunately, even if we do this call, the garbage collection trims the working set down anyway, bypassing MinWorkingSet (see "Automatic GC.Collect() in the diagram below).

Question: Is there a way to lock the 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.s. Every time a page fault occurs, it blocks the thread for 250us, which hits application performance badly.

enter image description here

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET