开发者

C# - Application Memory Problem

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 开发者_JAVA百科how much reduction is possible?


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? =)


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜