针对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。也许有人有任何想法?
答案 0 :(得分:1)
ManagementObjectSearcher
实施IDisposable
(ManagementObjectCollection
和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();
}
}
}
}
答案 1 :(得分:0)
根据上面michael-graczyk的评论,“ ...”处理中存在一个错误。
只需调用 GC.WaitForPendingFinalizers()即可解决
尽管我们并不真正认为它是一种解决方案,但这只是一种解决方法。