C# - RegistryKey。为什么没有收集垃圾?

时间:2017-03-01 06:44:27

标签: c# garbage-collection registrykey

我一直在试用SciTech的.NET Memory Profiler。它报告了一个问题,即RegistryKey实例已被Disposed而不是Garbage Collected。

enter image description here enter image description here

以下是有问题的代码,我无法弄清楚为什么没有收集垃圾。

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

0 个答案:

没有答案