Ravikiran
Ravikiran

Reputation:

How to reduce memory consumption by an application in Task manager

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.

Upvotes: 0

Views: 3541

Answers (4)

John Saunders
John Saunders

Reputation: 161773

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.

Upvotes: 0

Guy Starbuck
Guy Starbuck

Reputation: 21873

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.

Upvotes: 2

Brann
Brann

Reputation: 32376

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.

Upvotes: 2

Jhonny D. Cano -Leftware-
Jhonny D. Cano -Leftware-

Reputation: 18013

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.

Upvotes: 3

Related Questions