0

针对 Win32_PageFileUsage 类运行 WMI 查询会导致内存泄漏。在我的情况下,它每 5 分钟对 200 台服务器进行一次。大约 3 小时后,内存泄漏接近 10 GB。我认为这与该事实有关,即页面文件不存在。值为“0”。这是我的代码:

...
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);
}

系统为 Windows Server 2008 SP2。也许有人有任何想法?

4

2 回答 2

1

ManagementObjectSearcher实现IDisposableManagementObjectCollection和一样ManagementBaseObject)。你应该处理这些......也许有适当的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();
            }
        }
    }
}
于 2012-06-29T08:48:49.000 回答
0

根据 michael-graczyk 上面的评论,Dispose of ... 中有一个错误。</p>

调用GC.WaitForPendingFinalizers() 一次就足以修复它

虽然我们并不认为它真的是一个解决方案,只是一种解决方法。

于 2018-12-14T12:04:22.297 回答