Is it possible to track memory usage in a C# appli

2020-06-18 02:00发布

问题:

I'd like to write a simple application which keeps track of its current memory usage, number of created objects etc. In C++ I'd normally override the new operator, but for obvious reasons I can't do this in C#. Is there any way to do this without using a profiler?

回答1:

You might want to start with the Garbage Collector. MSDN has some members listed here that can show you how to do a few things, like get the total amount of memory it thinks is allocated, how many times the GC has collected. Anything more advanced than that, like getting a count of objects of your loaded assembly and you'll have to probably use a profiler or write something yourself.



回答2:

.NET Memory performance counters will give you aggregate information, but does not include object counters. For that you will need a profiler.



回答3:

Using WMI try :

To get process usage (W2K3/2K8) :

"SELECT IDProcess, PercentPrivilegedTime, PercentProcessorTime, PercentUserTime FROM Win32_PerfFormattedData_PerfProc_Process where Name='process_name.exe'"

To identify your site use this :

"SELECT ProcessId, CommandLine, WorkingSetSize, ThreadCount, PrivatePageCount, PageFileUsage, PageFaults, HandleCount, CreationDate, Caption FROM Win32_Process where Caption='process_name.exe'"

Use this tool for WQL teste

Or use PerfMon tool.

For more information about counters see Windows System Resource Manager Accounting, at the end of doc.

Good luck.