.NET EXE memory footprint

net

Even a simple Notepad application in C# consumes megabytes of RAM as seen in the task manager. On minimizing the application the memory size in the task manager goes down considerably and is back up when the application is maximized.

I read somewhere that the .NET process reserves a lot of memory for runtime allocation in advance. That's why .NET applications have a larger memory footprint to start with. But this memory can be released using Win32 API calls. A trade-off is that runtime allocation becomes slow – is that true?

Best Answer

The reason for the large memory footprint is that the JIT compiler and Windows Forms engine are being loaded with your process. To reduce this, you can do the following:

[DllImport("psapi.dll")]
static extern int EmptyWorkingSet(IntPtr hwProc);

static void MinimizeFootprint()
{
    EmptyWorkingSet(Process.GetCurrentProcess().Handle);
}

This should remove as much as possible from your memory footprint. There may be a way that you can also reduce the amount of memory that is set aside for runtime memory allocation.