定期 WMI 查询导致内存泄漏? [英] Periodical WMI Query Causes Memory Leak?

查看:37
本文介绍了定期 WMI 查询导致内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 .NET 编程还很陌生,我目前正在开发一个计算机健康监控系统,该系统现在还处于起步阶段.我将使用 C# 2010 并使用 WMI 查询来查询计算机信息.

I am fairly new to the .NET programming and I am currently developing a computer health monitoring system which is in its infant stage now. I will be using C# 2010 and querying computer information by using WMI queries.

在进一步开发应用程序之前,我创建了一个迷你测试应用程序来测试我的类及其方法.测试应用流程如下:

Before I could further develop the application, I have created a mini test app to test out my classes and its methods. The flow of the test app is as follow:

  1. 应用启动
  2. 输入主机名、用户名和密码
  3. 点击查询按钮并触发查询方法.
  4. 我的 UI 上的文本字段得到更新,打印出查询的结果.

我有一个名为 Machine 的类,其中包含诸如 CPU 名称之类的属性,以及一些可更新的属性,例如当前的 CPU 使用率.在那个类中,我有两个主要方法,GetStaticSysInfo 和 GetDynamicSysInfo,其中第一个方法查询不随时间变化的系统信息,后者查询 CPU 和内存使用情况等信息.我有另一个名为 Refresh 的方法,我用它来包装 GetDynamicSysInfo 方法.

I have a class called Machine, which contains properties such as the CPU Name, and some update-able properties like the current CPU usage. In that class, I have 2 main methods, GetStaticSysInfo and GetDynamicSysInfo, where the first method queries the system info that does not change over time, and the later one queries information like CPU and memory usage. I have another method named Refresh that I use to wrap around the GetDynamicSysInfo method.

当我在 UI 中使用 WPF 时,我使用 DispatcherTimer 定期查询机器,并在单击查询"按钮后将更新的信息打印到 UI 上的文本字段.但是,我注意到每次调用 machine.Refresh() 时,应用程序的内存使用量都会增加一点(几百 KB).我真的无法弄清楚该程序有什么问题,如果有人可以就此提供一些建议,我将不胜感激.

As I am using WPF for my UI, I have used the DispatcherTimer to periodically queries the machine, and prints the updated info to the textfield on the UI after the Query button has been clicked. However, I noticed that each of the time I called machine.Refresh(), the memory usage of the app increases by a bit (few hundred KBs). I can't really figure out what's wrong with the program and I would appreciate that someone could provide some advices on this.

如果您需要更多信息或代码的任何部分,请告诉我.

Please let me know if you need more information, or any portions of the code.

提前致谢.

我在 Timer_Tick 方法上添加了 GC.Collect(),似乎内存使用量仍在攀升,但每隔几个计时器滴答就会降低一次.它仍在增加,但速度较慢.这是正确的做法吗?从长远来看会影响性能吗?

I have added a GC.Collect() on the Timer_Tick method, and it seems like the memory usage still climbs but it is lowered once every few timer ticks. It is still increasing, but at a slower rate. Is this the correct way of doing it and will it impair performance in the long run?

推荐答案

除非出于诊断目的,否则您不应调用 GC.Collect().对于 GC 应该何时运行,运行时有更好的算法.此外,过于频繁地调用 Collect() 会导致对象被提升到第 1 代或第 2 代,这实际上意味着它们在不再使用后会被更慢地收集,即增加您的内存占用.

Except for diagnosis purposes you should never call GC.Collect(). The runtime has much better algorithms for when the GC should run. Also, calling Collect() too often can result in objects being promoted to 1st or 2nd generation, which actually means they are collected more slowly after they are not used anymore, i.e. increasing your memory footprint.

如果手动收集后内存再次下降(到以前的值),那么您没有内存泄漏.由于 GC 不会一直运行,因此您将在 GC 运行之间增加内存.这无需担心.

If the memory goes down again (to the previous value) after manually collecting, then you have no memory leak. Since the GC doesn't run all the time, you will have increased memory in between GC runs. That is no cause for concern.

这篇关于定期 WMI 查询导致内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆