小编beb*_*ebo的帖子

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

我在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 …
Run Code Online (Sandbox Code Playgroud)

c#

1
推荐指数
1
解决办法
2574
查看次数

标签 统计

c# ×1