轮询 C# 应用程序在运行时的内存使用情况?

发布于 2024-07-12 08:35:58 字数 104 浏览 4 评论 0原文

我有一个应用程序,在运行时需要轮询其自己的内存使用情况。 如果它能够列出每个实例化对象的内存使用情况,那就更理想了。 我知道这可以通过 WMI 来实现,但我希望有一些不依赖 WMI 的东西。

I have an app that, while running, needs to poll its own memory usage. It would be ideal if it could list out the memory usage for each object instantiated. I know this can be achieved by WMI, but I was hoping for something that doesn't rely on WMI.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

2024-07-19 08:35:58

您可能会发现有用的两个函数是:

  GC.GetTotalMemory();
  Process.PagedMemorySize64();

我的经验是 GC.GetTotalMemory() 并不是非常可靠。 它经常报告的内存使用量比实际内存使用量小得多。 我看到它报告说,当我的程序在 16 GB 机器上运行内存不足时,我只使用了 8 GB。

我还没有测试 Process.PagedMemorySize64,尽管它看起来确实很有希望。

Two functions you might find useful are:

  GC.GetTotalMemory();
  Process.PagedMemorySize64();

My experience has been that GC.GetTotalMemory() is not terribly reliable. It often reports memory usage that is much smaller than the actual memory usage. I've seen it report that I'm using only 8 gigabytes when my program runs out of memory on a 16 gigabyte machine.

I haven't yet tested Process.PagedMemorySize64, although it does look promising.

巷雨优美回忆 2024-07-19 08:35:58
Process proc = Process.GetCurrentProcess();
Logger.Info(proc.PeakWorkingSet64/1024 + "kb");
Process proc = Process.GetCurrentProcess();
Logger.Info(proc.PeakWorkingSet64/1024 + "kb");
情何以堪。 2024-07-19 08:35:58

您可以监听 perfmon 计数器,这将为您提供大量数据(GC 活动/物理内存使用情况/托管堆等。)

如果您需要更深入,您可能必须为自己附加一个调试器,这确实非常棘手因为你必须生成一个新进程并与其通信,并遍历你的内存。

You could listen on perfmon counters, which will give you plenty of data (GC activity / physical memory usage / managed heap etc..)

If you need to go deeper you will probably have to attach a debugger to yourself, which is really super tricky cause you will have to spawn a new process and communicate with it, and walk through your memory.

李不 2024-07-19 08:35:58

您可以从 Process 类 System.Diagnostics 获得有关流程的一些粗粒度信息。 http://msdn.microsoft.com/en-us/库/system.diagnostics.process.aspx

没有任何“每个对象”的东西,但至少可以收集一些有关您的进程的内存信息。

You can get some coarse granularity about your process from System.Diagnostics, the Process class. http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx.

None of the 'per object' stuff, but at least some memory info about your process can be gleaned.

情域 2024-07-19 08:35:58

或许

Windows::System::Diagnostics::ProcessDiagnosticInfo::GetForCurrentProcess();

Maybe

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