在运行时轮询 C# 应用程序的内存使用情况?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/463595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Poll C# app's memory usage at runtime?
提问by maxfridbe
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.
我有一个应用程序,它在运行时需要轮询自己的内存使用情况。如果它可以列出实例化的每个对象的内存使用情况,那将是理想的。我知道这可以通过 WMI 实现,但我希望有一些不依赖 WMI 的东西。
采纳答案by Jim Mischel
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.
我的经验是 GC.GetTotalMemory() 不是非常可靠。它通常报告的内存使用量远小于实际内存使用量。我已经看到它报告说,当我的程序在 16 GB 的机器上耗尽内存时,我只使用了 8 GB。
I haven't yet tested Process.PagedMemorySize64, although it does look promising.
我还没有测试 Process.PagedMemorySize64,虽然它看起来很有希望。
回答by Sam Saffron
You could listen on perfmon counters, which will give you plenty of data (GC activity / physical memory usage / managed heap etc..)
您可以监听 perfmon 计数器,这将为您提供大量数据(GC 活动/物理内存使用量/托管堆等。)
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.
如果您需要更深入地了解,您可能必须为自己附加一个调试器,这真的非常棘手,因为您必须生成一个新进程并与之通信,并遍历您的内存。
回答by Joe
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.
您可以从 Process 类 System.Diagnostics 中获得有关流程的一些粗粒度。 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.
没有任何“每个对象”的东西,但至少可以收集到一些关于你的进程的内存信息。
回答by user318554
Process proc = Process.GetCurrentProcess();
Logger.Info(proc.PeakWorkingSet64/1024 + "kb");
回答by Mo0gles
Maybe
也许
Windows::System::Diagnostics::ProcessDiagnosticInfo::GetForCurrentProcess();