简体   繁体   English

C#程序中的内存使用率过高

[英]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. 首先,我想说的是,我知道.NET程序的实际内存使用量和所声明的内存使用量并不相同。 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. 这些年来,我已经使用了许多.NET程序,并认为学习编程和对我发现的一些开源项目做出贡献会很有趣。

However in building my small app and getting some minimal function out of it is coming at a huge memory cost it seems. 但是,在构建我的小型应用程序并获得一些最低限度的功能时,似乎要付出巨大的内存成本。 It 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. 它约为40kb,其中包含一些图像,但是从20mb的内存开始,并且在执行了一些操作之后上升到30s的低位。

Compared to the other .NET applications I am running at the same time, it is over 3 times the size of them. 与我同时运行的其他.NET应用程序相比,它的大小是它们的3倍以上。

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. 我环顾了Visual Studio,发现一个设置从调试更改为发布,并尝试将其设置为发布,但没有发现任何区别。

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. 我想在尝试进行Windows Phone 7开发之前应该解决此问题,这是我的目标,因为那里的内存将是一个更大的问题。

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. 更新:该程序是一个RSS阅读器,它仅检查Twitter提要并将数据解析为具有基于hash标签的一些其他信息的列表框。 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. 我绝对同意Rob的看法,因为探查器会告诉您代码中是否存在内存泄漏或使用效率低下的问题。 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. 但是,您必须记住,即使您有一个小型的.NET应用程序,您(除您自己的代码外)还将加载CLR,JIT编译器,众多框架程序集等。此外,如果在分析了您的应用程序之后,GC预测您将需要大量堆内存,它可能会重新调整堆大小的大小以实现快速分配等((如果内存资源压力很大,可能会减小大小)。

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. 使用内存分析器(http://www.google.co.uk/search?q=dot+net+memory+profiler-我自己喜欢DotTrace),您应该能够找到造成此问题的原因。

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. 一种猜测是,在轮询RSS提要时,您会不断创建新对象,即使您最终没有“使用”它们中的大多数,它们仍然驻留在内存中,直到垃圾回收器来收集它们为止。 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 我最近遇到了类似的问题,发现使用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. 基本上,您可以了解应用程序实际使用了多少内存,以及Windows分配了哪些内存。 It is mostly a case of Windows and .NET allocating memory to make your application run better and reduce the amount of memory allocations. 在大多数情况下,Windows和.NET都会分配内存,以使您的应用程序更好地运行并减少内存分配量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM