Overly high memory usage in a C# program
I want to say at the start that I know that the actual memory usage and the claimed memory usage of a .NET program are not the same. I have used many .NET programs over the years and figured it would be fun to learn how to program and maybe contribute to some of the open source projects I have found.
However in building my small app and getting some minimal function out of it is coming at a huge memory cost it seems. It开发者_JAVA技巧 is ~40kb, with some images in it, but starts at 20mb of memory and after doing a few of the actions in it goes up to the low 30s.
Compared to the other .NET applications I am running at the same time, it is over 3 times the size of them.
I thought there may be something I am missing as I am new to programming. I have looked around Visual Studio and found a setting that changes from debug to release and have tried publishing with that on but did not notice a difference.
Are there some other settings I am missing? Or am I not writing my code correctly to account of this huge discrepancy? Figured I should solve this before I try to work with Windows Phone 7 development, which was my goal, as memory would be a much bigger issue there.
Update: Program is a RSS reader that just checks a twitter feed and parses the data into a list box with some additional information based on the hash tag. I am hoping there is a free, or very low cost solution to this issue as this was just a hobby I picked up and so the high cost professional grade profilers programs seem to be just too pricey for my intentions.
I definitely agree with Rob in that a profiler will tell you if there are any memory leaks or usage inefficiencies in your code. However, you must remember that even if you have a small .NET application, you will (in addition to your own code) be loading the CLR, the JIT compiler, numerous framework assemblies etc etc. Also, if after analyzing your application the GC predicts that you'll need a lot of heap memory it may re-size the heap size upwards to allow for fast allocations etc, (which it could reduce in size if there is pressure on memory resources.
So in summary, an apparently large running memory allocation may not indicate any problems, but profiling will allow you to double-check this issue.
Use a memory profiler (http://www.google.co.uk/search?q=dot+net+memory+profiler - i like DotTrace myself), and you should be able to find what's causing this.
Profiling is the best way, because otherwise you're might "fix" something that wasn't even broken
One guess is that your constantly creating new objects when polling your RSS feeds, and even though you not end up "using" most of them they still reside in memory until the Garbage Collector comes around to collect them. It's not a problem per se but it might make your application seem to take up more memory.
I encountered a similar issue recently and found it helpful to use this free tool from Sysinternals: VMMAP
It basically gives you an idea of how much memory is actually being used by the application and where vs what Windows is allocating. It is mostly a case of Windows and .NET allocating memory to make your application run better and reduce the amount of memory allocations.
精彩评论