简体   繁体   中英

c# windows app - profiling peak memory usage and identifying trends

I have a long running console app running through millions of iterations. I want to benchmark if memory usage increases linerally as the number of iterations increases .

What would be the best way to go about this?

I think I really only need to be concerned with Peak memory usage during a run right? I basically need to work out what is the max number of iterations I can run on this hardware given the memory on the Server.

I am going to setup a batch lot of runs and Log the results over different interation sizes and then graph the results to identify a memory usage trend which can then be extrapolated for any given hardware.

Looking for advice on the best way to implement this, what .net methods, classes to use or should I use external tools. This article http://www.itwriting.com/dotnetmem.php suggests I should profile my own app via code to factor out shared memory used by the .net runtime across other apps on the box.

Thanks

There are several ways to do this:

Perfmon UI

You can use the Performance Montior control panel applet (in Administrative Tools) that comes with Windows to monitor your application. Have a look at the .Net CLR Memory category and the counters inside it. You can also limit the monitoring to just your process. This is probably the easiest as it requires no code changes.

Perfmon API

You can programmatically use performance counters from .Net. For this, you'll need to use the PerformanceCounter class. This is just an API to the same underlying information that the above UI presents.

Memory Profiler

You can use a memory profiler to profile you application whilst it is running. The two I have used with success are the ANTS Memory Profiler from RedGate and the .Net Memory Profiler from SciTech. Again, this requires no code changes, but may cost you money (although there are free trial versions). There is also the CLR Profiler (a howto can be found here ).

Roll Your Own

You can get hold of some limited memory information from the Process class. Get hold of the current process using Process.GetCurrentProcess() , and then look at the properties of it, specifically the ones relating to memory ( MinWorkingSet , MaxWorkingSet , PagedMemorySize64 , PeakPagedMemorySize64 , PeakVirtualMemorySize64 , PeakWorkingSet64 , PrivateMemorySize64 , VirtualMemorySize64 , WorkingSet64 ). This is probably the worst solution as you have to do everything yourself, including data collection and reporting.


If all you are wanting to do is to verify your application doesn't linearly increase its memory usage as your the number of iterations increases, I would recommend monitoring using the Performance Monitor UI in Windows. It will show you what you need with minimal effort on your part.

Windows perfmon is great for this kind of stuff. You have counters for all the managed heaps and for private bytes, if you need to cross correlate with iteration counts you can publish your own perfmon counters from .Net.

when you want to profile the mem usage of your apps, here is a piece of code you can use. First you have to declare an instance of the PerformanceCounter class

Assembly a = Assembly.GetExecutingAssembly();
_PerfCounter = new PerformanceCounter(".NET CLR Memory",
                                      "# Bytes in all Heaps",
                                      a.GetName().Name,
                                      true);

and each time you want to log the memory consumption you can call

_PerfCounter.NextValue()

Hope it was helpful

也许这个工具可以做你想要的一切。

迟到总比没有好,但是为了响应M3ntat的注释,如果从Visual Studio中运行,则需要在程序集名称参数中添加“.vshost”,例如

a.GetName().Name + ".vshost"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM