简体   繁体   English

在Server 2008 SP2上对Win32_PageFileUsage类运行WMI查询时出现内存泄漏

[英]Memory leak while running WMI query to Win32_PageFileUsage class on Server 2008 SP2

Running WMI query against Win32_PageFileUsage class causes a memory leak. 对Win32_PageFileUsage类运行WMI查询会导致内存泄漏。 In my situation it is being done to 200 servers every 5 minutes. 在我的情况下,每5分钟对200台服务器进行一次处理。 After about 3 hours the memory leak is nearly 10 GB. 大约3小时后,内存泄漏将近10 GB。 I think it is somehow relaited to that fact, that the pagefile does not exist. 我认为页面文件不存在与该事实有某种关系。 The value is "0". 值为“ 0”。 Here is my code: 这是我的代码:

...
ObjectQuery pageFileUsageQuery = 
        new ObjectQuery("SELECT AllocatedBaseSize, CurrentUsage FROM Win32_PageFileUsage");
m_PageFileUsageSearcher = new ManagementObjectSearcher(managementScope, pageFileUsageQuery);
...
var pageFileUsageCollection = m_PageFileUsageSearcher.Get();
double currentUsage = 0;
double maxSize = 0;

foreach (ManagementBaseObject managementBaseObject in pageFileUsageCollection)
{
        string result = managementBaseObject["CurrentUsage"].ToString();
        currentUsage += double.Parse(result);
}

The system is Windows Server 2008 SP2. 该系统是Windows Server 2008 SP2。 Maybe somebody has any ideas? 也许有人有什么想法?

ManagementObjectSearcher implements IDisposable (as does ManagementObjectCollection and ManagementBaseObject ). ManagementObjectSearcher实现IDisposable (以及ManagementObjectCollectionManagementBaseObject )。 You should dispose of these... perhaps with well placed using statements. 您应该处理这些...也许可以using语句放置得很好。

ObjectQuery pageFileUsageQuery = 
        new ObjectQuery("SELECT AllocatedBaseSize, CurrentUsage FROM Win32_PageFileUsage");
using(m_PageFileUsageSearcher = new ManagementObjectSearcher(managementScope, pageFileUsageQuery))
{
    ...
    using(var pageFileUsageCollection = m_PageFileUsageSearcher.Get())
    {
        double currentUsage = 0;
        double maxSize = 0;

        foreach (ManagementBaseObject managementBaseObject in pageFileUsageCollection)
        {
            try
            {
                string result = managementBaseObject["CurrentUsage"].ToString();
                currentUsage += double.Parse(result);
            }
            finally
            {
                managementBaseObject.Dispose();
            }
        }
    }
}

As per comment above by michael-graczyk, there is a bug in the Dispose of … 根据以上michael-graczyk的评论,Dispose of…中存在一个错误。

Calling GC.WaitForPendingFinalizers() once is enough to fix it 一次调用GC.WaitForPendingFinalizers()足以修复它

though we do not consider it a solution really, just a workaround. 尽管我们实际上并不认为这是一个解决方案,但这只是一种解决方法。

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

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