根据 C# 对象查找进程内存分配的最佳方法是什么
我编写了各种基于 C# 控制台的应用程序,其中一些运行时间较长,有些则不运行,随着时间的推移,它们会占用大量内存。当通过任务管理器查看 Windows 性能监视器时,同样的问题不断出现在我的脑海中;如何按类型细分造成此足迹的对象数量;其中哪些是 f-reachable 的,哪些是不可到达的,因此可以被收集。在很多情况下,我都执行了代码检查,以确保我不会不必要地持有超过所需时间的对象,并使用 using 构造来处置对象。我最近还考虑在释放大量对象(例如保存在刚刚被清除的集合中)时使用 CG.Collect 方法。但是,我不太确定这会产生那么大的差异,所以我扔掉了该代码。 我猜测 sysinternals 套件中有一些工具可以帮助解决这些内存类型问题,但我不确定哪些工具以及如何使用它们。另一种方法是付费购买第三方分析工具,例如 JetBrains dotTrace;但我需要确保在向我的经理提供上限之前我已经探索了免费选项。
I have written various C# console based applications, some of them long running some not, which can over time have a large memory foot print. When looking at the windows perofrmance monitor via the task manager, the same question keeps cropping up in my mind; how do I get a break down of the number objects by type that are contributing to this footprint; and which of those are f-reachable and those which aren't and hence can be collected. On numerous occasions I've performed a code inspection to ensure that I am not unnecessarily holding on objects longer than required and disposing of objects with the using construct. I have also recently looked at employing the CG.Collect method when I have released a large number of objects (for example held in a collection which has just been cleared). However, I am not so sure that this made that much difference, so I threw that code away.
I am guessing that there are tools in sysinternals suite than can help to resolve these memory type quiestions but I am not sure which and how to use them. The alternative would be to pay for a third party profiling tool such as JetBrains dotTrace; but I need to make sure that I've explored the free options first before going cap in hand to my manager.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CLR Profiler 可让您查看各种对象图(我从未使用过它):
http://www.microsoft.com/downloads/details.aspx?FamilyID=86ce6052-d7f4-4aeb-9b7a-94635beebdda&displaylang=en
当然,ANTS Profiler(免费试用)是经常推荐的分析器。您不需要手动进行垃圾收集,GC 是一个非常智能构建的解决方案,可能比您所做的任何手动调用都更优化。更好的方法是尽量减少内存中保存的对象数量 - 如果内存优先,请尽快删除占用大量内存的对象。
There is the CLR Profiler that lets you review various object graphs (I've never used it):
http://www.microsoft.com/downloads/details.aspx?FamilyID=86ce6052-d7f4-4aeb-9b7a-94635beebdda&displaylang=en
Of course, ANTS Profiler (free trial) is an often recommended profiler. You shouldn't need to manually garbage collect, the GC is a very intelligently built solution that will likely be more optimal than any manual calls you do. A better approach is to be minimalist with the number of objects you keep in memory - and be rid of memory-heavy objects as soon as possible if memory is priority.