views:

52

answers:

3

I have written a small WinForm application in C#. The EXE that is made is 74 Kb and all together with the resources and all it sizes 179 Kb. But when I run it, it takes 9.1 MBs in memory according to Task Manager.

So my question is:

  • Why is it happening?
  • What can I do to reduce the size of this?
  • If the size could be reduced how much reduction is possible?
+3  A: 

Firstly, using Task Manager to determine memory usage is fraught with peril. Have a read of this article to gain a clearer understanding of it.

  • What other libraries are you referencing?
  • What files are you loading, into streams or any other way?
  • What resources do you request from the Operating System via P/Invoke
  • How many instances of each form do you load?
  • How many variables do you create, and of what size?

For example:

// Will take up a lot of memory!
var x = new byte[int.MaxValue];

All that said, 9.1Mb isn't really a lot (less than 0.5% of memory on a machine spec'd with 2Gb of RAM) and more importantly, does it actually matter that your application's using 9.1Mb of RAM, or are you wasting your time investigating? Remember, your time's valuable. Would your end users rather the time was spent on something else? =)

Rob
nothing its just on simple form.. which has a picture box that displays an image from local drive according to the day of the week..but there is one thing i have set the transparency key such that my images (PNGs) don't show their transparent areas...thats it...
Junaid Saeed
thanks the article helped... but still is there a way to optimize memory utilization by application.. not via my running code that i can handle... something like when solution is built and stuff like that... just wondering
Junaid Saeed
With the information you've given us, no there's not. I'd suggest there's probably no need to either =)
Rob
One of the hardest bits of developing software, especially for complex GUI applications, is memory allocation. C#, other .NET languages and Java have their own memory manager in a virtual machine that takes care of this for you. If you really do need this degree of low level control -- and remember, 10Mb is less than 0.5% of memory on a machine with 2Gb -- then look at languages where you do have more control over memory allocation such as C or C++.
Jeremy McGee
@Jeremy, +1 from me, I wish I'd thought to express 10Mb as a percentage of 2Gb, it puts it into perspective much more clearly than my "isn't really a lot" =)
Rob
guys i know 10 Mb is nothing... but considering my target market i need to optimize my solutions for PCs which mostly have 256 to 512 MB memory. And are running XP 2003 SP2 along with the diversity of anti viruses running on their machines... and a lot of other software that they have like desktop sidebar etc, big time memory black holes... you know what i am trying to say...
Junaid Saeed
plus 74 Kb * 100 = 7.2 MB < 10 MBimagine if my app size was 1 MB1 * 100 = 100 MB < actual size in memory...get the point... good.. lets dance
Junaid Saeed
+1  A: 

Size of executable and memory usage are two completely separate notions. For example this simple program:

class Program
{
    static void Main()
    {
        var b = new byte[int.MaxValue];
    }
}

is only 4KB but it uses all of the available RAM on your computer and crash. This is to demonstrate you that you could have an extremely simple application but depending on what it is doing it could consume lots of memory. So what is your application doing?

Darin Dimitrov
A: 

The memory usage of a program is not 100% related to the size of it's binary or resources.

It depends on what your program does. For example if you create something like this:

List<int> list = new List<int>();
for (i=1; i<100000; i++) list.Add(i);

It will take as much memory as it need to store int's plus it's object overhead.

And it depends on what usings you've used.

You've tagged your post with winforms - I assume you've got a gui app. Gui's memory usage depend on used controls and their gui style (e.g. animations, hover effects ...)

And .NET has a garbage collector which will free unused memory during runtime.

Andreas Rehm