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?

3 Answers
3

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.

Is there any trade off in emptying the working set.
–
dotnetcoderOct 21 '08 at 20:10

I'd like to add a note of thanks about this ... I know that working set isn't a valid representation, but that number tends to scare administrators. This tip just took one of my apps in development from an average 80 - 120 meg working set down to 20 - 40. +1
–
John RudyOct 21 '08 at 20:29

1

dotnetcoder--the trade-off is that you will incur a performance hit if your application needs any of the resources you freed, which is relatively likely.
–
Ed AltorferOct 21 '08 at 21:12

John Rudy--I know how that can be; someone sees your app taking "tons of space" and freaks. I'm glad that this helped you a bit. :)
–
Ed AltorferOct 21 '08 at 21:15

4

Good idea, just educate the entire world, especially software reviewers. That's a practical solution. Or perhaps not. Seems like it'd be better to take a (probably imperceptible) performance hit.
–
mhenry1384May 1 '09 at 18:06