C# Windows 应用程序 - 分析峰值内存使用情况并识别趋势
我有一个长时间运行的控制台应用程序,经过数百万次迭代。 我想基准测试内存使用量是否随着迭代次数的增加而线性增加。
解决这个问题的最佳方法是什么?
我想我真的只需要关心运行期间的峰值内存使用,对吧? 我基本上需要计算出在给定服务器内存的情况下我可以在该硬件上运行的最大迭代次数是多少。
我将设置一批运行并记录不同交互大小的结果,然后绘制结果以识别内存使用趋势,然后可以针对任何给定硬件进行推断。
寻求有关实现此目的的最佳方法、要使用哪些 .net 方法、类或者我应该使用外部工具的建议。 这篇文章 http://www.itwriting.com/dotnetmem.php 建议我应该介绍一下我的自己的应用程序通过代码分解.net运行时使用的共享内存在盒子上的其他应用程序中。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有多种方法可以执行此操作:
Perfmon UI
您可以使用 Windows 附带的 Performance Montior 控制面板小程序(在管理工具中)来监视您的应用程序。 查看 .Net CLR Memory 类别及其内部的计数器。 您还可以将监控仅限于您的进程。 这可能是最简单的,因为它不需要更改代码。
Perfmon API
您可以通过编程方式使用 .Net 中的性能计数器。 为此,您需要使用PerformanceCounter班级。 这只是一个 API,其底层信息与上述 UI 所呈现的相同。
内存分析器
您可以使用内存分析器在应用程序运行时对其进行分析。 我成功使用过的两个是 RedGate 的 ANTS Memory Profiler 和来自 SciTech 的 .Net 内存分析器。 同样,这不需要更改代码,但可能会花费您金钱(尽管有免费试用版)。 还有CLR Profiler< /a>(可以在此处找到操作指南)。
自己动手
您可以从进程中获取一些有限的内存信息 类。 使用 Process.GetCurrentProcess()< 获取当前进程/a>,然后查看它的属性,特别是与内存相关的属性(MinWorkingSet, MaxWorkingSet,PagedMemorySize64,< a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.peakpagedmemorysize64.aspx" rel="noreferrer">PeakPagedMemorySize64,PeakVirtualMemorySize64,PeakWorkingSet64,PrivateMemorySize64,VirtualMemorySize64,工作集64)。 这可能是最糟糕的解决方案,因为您必须自己完成所有事情,包括数据收集和报告。
如果您想要做的只是验证您的应用程序不会随着迭代次数的增加而线性增加其内存使用量,我建议您使用 Windows 中的性能监视器 UI 进行监控。 它会告诉您您需要什么,您只需付出最少的努力。
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 非常适合这种情况东西。 您拥有所有托管堆和私有字节的计数器,如果您需要与迭代计数交叉关联,您可以 从 .Net 发布您自己的 perfmon 计数器。
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.
当您想要分析应用程序的内存使用情况时,可以使用以下一段代码。
首先,您必须声明 PerformanceCounter 类的实例
,每次您想要记录内存消耗时,您可以调用
希望它有帮助
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
and each time you want to log the memory consumption you can call
Hope it was helpful
也许这个工具可以做你想做的一切。
Maybe this tool can do everything you want.
迟到总比不到好,但为了响应 M3ntat 的评论,如果您从 Visual Studio 中运行,则需要将“.vshost”添加到程序集名称参数中,例如
Better late than never, but in response to M3ntat's comment, you'll need to add ".vshost" onto the assembly name parameter if you're running from within Visual Studio, e.g.