在Dictionary中处理类的问题尽管使用了GC.Collect,它仍然在堆内存中

beb*_*ebo 1 c#

我在Dictionary中处理类时遇到问题

这是我的代码

  private Dictionary<string, MyProcessor> Processors = new Dictionary<string, MyProcessor>();

        private void button1_Click(object sender, EventArgs e)
        {
            if (!Processors.ContainsKey(textBox1.Text))
            {
                Processors.Add(textBox1.Text, new MyProcessor());
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            MyProcessor currnt_processor = Processors[textBox2.Text];
            Processors.Remove(textBox2.Text);
            currnt_processor.Dispose();
            currnt_processor = null;
           GC.Collect();
            GC.WaitForFullGCComplete();
        }








  public class MyProcessor: IDisposable
    {

       private bool isDisposed = false;

       string x = "";

        public MyProcessor()
        {
            for (int i = 0; i < 20000; i++)
            {
            //this line only to increase the memory usage to know if the class is dispose or not
                x = x + "gggggggggggg";    

            }

        }

        public void Dispose()
        {
            x=null;
            this.Dispose(true);
            GC.SuppressFinalize(this); 
        }

        public   void Dispose(bool disposing)
        {
            if (!this.isDisposed)
            {
                isDisposed = true;
                this.Dispose();
            }
        }

        ~MyProcessor()      
        {
            Dispose(false);
        }


    }
Run Code Online (Sandbox Code Playgroud)

我使用"ANTS Memory Profiler"监视堆内存

只有当我从字典中删除所有键时才处理工作

我如何从堆内存中销毁类?

这是一个问题的视频链接

http://www.youtube.com/watch?v=ePorlksv2QY

提前致谢

Bro*_*ass 5

我认为你看到鬼 - 请记住,.NET垃圾收集是基于内存压力的分代垃圾收集.如果没有内存压力,您的资源将不会被垃圾收集.调用GC.Collect()也只是一个坏主意,我希望你只是为你的分析测试做这个.

另外,您的Dispose方法中处理的资源究竟是什么?看起来不像你需要一个.

在实现中,您提供的不是单个Dispose()方法调用,也不~MyProcessor()需要终结器.