problem disposing class in Dictionary it is Still in the heap memory although using GC.Collect

Posted by Bahgat Mashaly on Stack Overflow See other posts from Stack Overflow or by Bahgat Mashaly
Published on 2011-01-15T01:50:31Z Indexed on 2011/01/15 1:53 UTC
Read the original article Hit count: 536

Filed under:

Hello

i have a problem disposing class in Dictionary

this is my code

  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[textBox1.Text];
            Processors.Remove(textBox2.Text);
            currnt_processor.Dispose();
            currnt_processor = null;
            GC.Collect(); 
        }








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

            }
            this.Dispose();
            GC.SuppressFinalize(this); 
        }

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

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

        ~MyProcessor()      
        {
            Dispose(false);
        }


    }

i use "ANTS Memory Profiler" to monitor heap memory

the disposing work only when i remove all keys from dictionary

how can i destroy the class from heap memory ?

thanks in advance

© Stack Overflow or respective owner

Related posts about c#