定期WMI查询导致内存泄漏?

发布于 2024-11-06 01:52:14 字数 796 浏览 1 评论 0原文

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

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

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

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

由于我在 UI 中使用 WPF,因此我使用 DispatcherTimer 定期查询计算机,并在单击“查询”按钮后将更新的信息打印到 UI 上的文本字段。但是,我注意到每次调用 machine.Refresh() 时,应用程序的内存使用量都会增加一点(几百 KB)。我真的无法弄清楚该程序出了什么问题,我希望有人能就此提供一些建议。

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

提前致谢。

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

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. App startup
  2. Input hostname, username and password
  3. Query button clicked and the querying methods fired.
  4. A textfield on my UI gets updated, printing out the result of the queries.

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.

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.

Thanks in advance.

EDIT: 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿越时光隧道 2024-11-13 01:52:14

除了诊断目的之外,您永远不应该调用GC.Collect()。对于 GC 何时运行,运行时有更好的算法。此外,过于频繁地调用 Collect() 可能会导致对象被提升为第一代或第二代,这实际上意味着它们在不再使用后收集速度会更慢,即增加内存占用。

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文