views:

686

answers:

4

Hi

I have developed a winforms application using C# in VS2008. When I run this application I observed in task manager that it shows 80 MB of memory is consumed. How can I reduce this? Even very small applications also take 8 MB of memory...

What can I do to reduce the memory footprint?

Thank you very much in advance.

+1  A: 

The task manager memory number is not always as sharp as it would be, however this small trick can cheat that number... Not meant for production

public static void RefreshMemory() {
    try {
     Process curProc = Process.GetCurrentProcess();
     curProc.MaxWorkingSet = curProc.MaxWorkingSet;
    } catch {
     // Handle the exception
    }
}

It would be helpful also to trace the memory objects usage with a tool like JetBrains dotTrace or another similar.

Jhonny D. Cano -Leftware-
Using a profiler to see what's taking up all the memory would probably be the best option.
Kibbee
I would think this hack only causes memory to be paged out, so in effect it does not actually reduce the memory consumption. In Task Manager (on XP) this does reduce the 'Mem Usage' value, but the 'VM Size' value just stays the same.So for user perception, this little hack does help in making the lesser educated users believe your app is a good citizen.In fact I do use the hack to avoid questions from some users. But I only apply it when the app has been idle for some time, like being minimized, or reduced to a Tray Icon.
Rudi
+1  A: 

Well, the .NET framework has a fairly significant overhead -- the simplest possible "hello world" console app has a 4 mb working set, as you have observed. There are a number of things you can do to reduce the memory footprint (reduce embedded resources, be sure to build in Release configuration, etc.)

But at the end of the day, .NET is designed for developer efficiency over memory/resource efficiency, so if you have an app that has to run in a very small efficient memory space you should consider writing it in C++ or some other language where you manage your own resources.

Guy Starbuck
fewer includes will have 0 impact on C# code.
Joel Coehoorn
+2  A: 

Your question clearly smells of premature optimization.

You should tackle memory usage only in the following situations :

  1. You're developing for a device which hasn't a lot of memory available (with the portable .net framework for example)
  2. You're reaching the limit of .net in term of memory size (ie around 1.3gbp)
  3. Your customers are complaining.

Don't get me wrong, this doesn't mean you must waste memory or that you shouldn't take memory in consideration while coding. I just mean that in your case, memory usage is probably not a big deal.

Brann
A: 

In addition to all the previous good comments, please remember that the numbers you are seeing in Task Manager are virtual memory, not physical memory. The actual amount of physical RAM used by the program is not obvious from looking at Task Manager. Also, Task Manager is showing you a series of snapshots - if you really had a problem, you'd want to look at it over time, with Perfmon or something, in addition to the excellent suggestion on profiling with JetBrains' dotTrace.

But don't do any optimization until your code works, and has high test coverage, and until you know what actually needs to be optimized. Otherwise, you run the risk of optimizing problems that don't exist, and worse, of optimizing the wrong problem, while ignoring the real problem or possibly making it worse.

John Saunders