根据 C# 对象查找进程内存分配的最佳方法是什么

发布于 2024-09-05 16:07:15 字数 422 浏览 5 评论 0原文

我编写了各种基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开半夏魅人心 2024-09-12 16:07:15

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文