我一直在试用SciTech的.NET Memory Profiler。它报告了一个问题,即RegistryKey实例已被Disposed而不是Garbage Collected。
以下是有问题的代码,我无法弄清楚为什么没有收集垃圾。
private string CheckForServer(RegistryKey key)
{
string server = null;
if(key != null)
{
var val = key.GetValue("Server");
if (val != null)
{
server = val.ToString();
}
}
return server;
}
public string GetServer()
{
string server = string.Empty;
using (RegistryKey localMachine = Registry.LocalMachine)
{
using (RegistryKey key = localMachine.OpenSubKey(@"SOFTWARE\xxxx\xxxx\Database"))
{
server = CheckForServer(key);
}
}
GC.Collect(); //I put this here to just see what would happen
//..null checking on server required
return server;
}