.NET EXE memory footprint

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.


TaskManager should not be used to measure the memory footprint of a .NET application.

When a .NET application starts, it asks the OS for a chunk of memory which it then segments to become the managed heap, stack, and large object heap. It is this total chunk of memory that TaskManager is reporting, which may or may not be completely used by .NET. Once a .NET application is given a chunk of memory, it will not release it until asked by the OS, which will only happen with the OS determines a need for more memory resources.

If you want to measure memory allocations, you need to look at the various performance monitor (PerfMon) counters.

You can use interop code to call Win32 APIs to trim your working set size, but the next time your application requests memory from the OS the working set will go back up and there will be a performance hit while the OS allocates and hands out the additional memory and the .NET runtime "configures" it.