Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are .NET Applications naturally memory intensive?

Tags:

c#

memory

I started to write a large application in C# for the first time in my life. I wrote sample module to test the idea behind my software. This module contained several dozen C# dictionaries and lists of objects that each had several members and properties.

I was shocked that after initializing core objects it end up utilizing about 40MB of RAM.

I tested and found out that more than 30MB is allocated after object initialization, but I was under the impression that given the size of my objects, no more than a few hundred kilobytes should have been consumed.

Have I done something wrong or is .NET naturally memory intensive compared to native code applications?

like image 696
PiotrK Avatar asked Oct 08 '09 21:10

PiotrK


4 Answers

How did you determine how much memory that was used? .NET applications are known to eagerly reserve more memory than they need, as long as there is plenty of memory available, and release memory back to the system if it starts to run short on memory.

I think you may get some pointers in this MSDN article.

like image 189
Fredrik Mörk Avatar answered Nov 05 '22 05:11

Fredrik Mörk


Using Task Manager to look at your memory usage is likely to be horrendously inaccurate.

Instead, grab a proper memory profiling tool (dotTrace is very good, and has a 10 day trial) and take the time to see your actual memory consumption.

The sorts of things I've seen in my own code include

  • Underestimating how much memory I'm actually using (not counting separate objects properly, not allowing for lists to have "spare" capacity)
  • Keeping references to transient objects that I don't need
  • Not allowing for transient objects created during operation that haven't yet been garbage collected
  • Not allowing for unallocated memory - memory that has been claimed from the OS (and therefore shows as a part of the process in Task Manager) but which has't been allocated to any one object yet
  • Not allowing for thread stacks (each thread gets a stack of its own; .NET applications are always multi-threaded as the framework may create several threads of its own).
like image 36
Bevan Avatar answered Nov 05 '22 03:11

Bevan


In the days where we have several GB of RAM on a machine, the idea that a user-facing app like you would generally build in C# should only use a few 100K of ram is backwards. Any unused ram on your system is wasted.

With that in mind, C# allocates memory using very different strategy from C++, such that larger chunks are allocated and freed less often.

That said, 40 seems a little high. I'm used to something closer to 10-14Mb for very simple console apps. Maybe there's a form taking up ram, or maybe I'm on a different version of the framework (2.0).

like image 35
Joel Coehoorn Avatar answered Nov 05 '22 03:11

Joel Coehoorn


The .Net runtime has a certain large overhead - we've found that even simple applications will tend to use much more memory that similar applications written in C++. Fortunately this overhead is quickly disipated in the noise as the size of the overall code increases. The second factor is that of garbage collection, the garbage collector runs "whenever", so by comparison to C++, memory allocations are not typically freed right away, but rather when it feels the requirement to do so.

like image 20
1800 INFORMATION Avatar answered Nov 05 '22 03:11

1800 INFORMATION