简体   繁体   English

C#如何报告非特定内存使用情况

[英]C# How to report nonspecific memory usage

So I'm trying demonstrate to my uppers that the product contains a memory leak. 因此,我试图向我的鞋帮证明该产品包含内存泄漏。 However, it takes about 2 hours of running a script that touches a COM object to duplicate up to an OutOfMemoryException . 但是,运行接触COM对象的脚本大约要花费2个小时才能复制到OutOfMemoryException In order to make this presentable, I'll need data for a baseline to show that it's not my script itself that's causing the memory problems, as well as the data to show that the behavior indeed duplicates a memory leak. 为了使这种表现形式具有可表示性,我需要基线数据来表明不是我的脚本本身导致了内存问题,还需要数据来表明行为确实重复了内存泄漏。

I plan to do this via a periodic report of total memory usage pooped out into a log file. 我打算通过定期报告将总内存使用情况报告到日志文件中来做到这一点。 For example, on this box I my Windows Task Manager -> Performance tab shows that I'm currently using 1.67GB out of 2.00GB. 例如,在此框上,我的Windows任务管理器->性能选项卡显示2.00GB中的当前我使用的是1.67GB。 That's the number I need to pull into my code and dump in a log file periodically. 那就是我需要输入我的代码并定期转储到日志文件中的数字。

Only one problem... how do I get that piece of information? 只有一个问题...我如何获得那条信息?

Thanks for any help you can provide, even if it's to tell me it's impossible :P. 感谢您可以提供的任何帮助,即使是告诉我不可能:P。

UPDATE : Thanks for the info on COM's memory issues, but the "baseline" of which I spake also touches the COM object in effectively identical ways and doesn't cause memory issues on the order of magnitude that a specific behavior does. 更新 :感谢您提供有关COM内存问题的信息,但是我所说的“基准”也以有效相同的方式触及COM对象,不会引起特定行为造成的内存问题。 Only answers to the question I posed would be helpful to me here. 在这里,仅回答我提出的问题将对我有所帮助。

Update: , In answer to the OP's question, class System.GC has a method for getting an estimate of the amount of memory in use: 更新: ,为了回答OP的问题, System.GC类提供了一种估计使用中的内存量的方法:

System.GC.GetTotalMemory(false)

If you are using COM on a long-running process (ie no idle time) then you will experience a memory leak unless you periodically call: 如果在长时间运行的进程(即没有空闲时间)上使用COM,则除非定期调用,否则遇到内存泄漏。

Thread.CurrentThread.Join(100);

The 100 can of course be changed, but will be how long your active thread "sleeps" before resuming. 100当然可以更改,但是它将是活动线程在恢复之前“休眠”的时间。 From the docs: 从文档:

Blocks the calling thread until a thread terminates or the specified time elapses, while continuing to perform standard COM and SendMessage pumping. 阻止调用线程,直到线程终止或经过指定的时间为止,同时继续执行标准COM和SendMessage泵送。

It is that last clause that is key. 关键是最后一个子句。

Reference: http://support.microsoft.com/kb/828988 参考: http : //support.microsoft.com/kb/828988

If a console application that is based on a single-threaded apartment (STA) creates and then uses STA Component Object Model (COM) components and the console application does not perform sufficient operations to pump COM messages, such as calling the Monitor.Enter method, the Thread.Join method, and others, the following symptoms may occur. 如果基于单线程单元(STA)的控制台应用程序创建然后使用STA组件对象模型(COM)组件,并且该控制台应用程序没有执行足够的操作来泵送COM消息,例如调用Monitor.Enter方法,Thread.Join方法等,可能会出现以下症状。 Also, if the console application performs operations that that run for a long time and that do not pump messages, such as calling the Console.ReadLine method, the following symptoms may occur: 此外,如果控制台应用程序执行了长时间运行且不会发送消息的操作,例如调用Console.ReadLine方法,则可能会出现以下现象:

  • The release of COM components may be delayed. COM组件的发布可能会延迟。
  • The calls to the Finalize methods of the objects that the garbage collector collects may be delayed. 垃圾收集器收集的对象的Finalize方法的调用可能会延迟。
  • Calls to COM components may block the application thread for extended periods. 调用COM组件可能会长时间阻止应用程序线程。 The memory amount that the STA application process uses may increase over time. STA应用进程使用的内存量可能会随时间增加。
  • Calls to the GC.WaitForPendingFinalizers method may take a long time to return. 调用GC.WaitForPendingFinalizers方法可能需要很长时间才能返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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