Reducing Memory Footprint of any Process with EmptyWorkingSet
Ever wondered why does .NET winform application takes so much memory on startup ? It is obvious that most of this memory is not in use. The reason is that JIT compiler and the whole winforms engine is loaded into the process. However it is possible to remove as many pages as possible from the working set of the specified process using EmptyWorkingSet from psapi.dll.
[DllImport("psapi.dll")] static extern int EmptyWorkingSet(IntPtr hwProc);
From managed code we can use it like so (for the current process):
static void ClearMemory() { EmptyWorkingSet(Process.GetCurrentProcess().Handle); }
However it is possible to use this function for any process which handle must have the PROCESS_QUERY_INFORMATION and PROCESS_SET_INFORMATION access rights. Be careful using EmptyWorkingSet since the process will allocate the memory again in the future when needed.
Sunday, April 20, 2008 9:07 PM