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

  • Question

  • 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[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()
        {
          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 ?

    this a video link for the problem

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

    thanks in advance

    • Moved by Paul Zhou Thursday, January 20, 2011 7:42 AM CLR (From:Visual C# General)
    • Moved by Cookie Luo Thursday, January 20, 2011 7:48 AM (From:.NET Base Class Library)
    Saturday, January 15, 2011 12:07 PM

All replies

  • Have you disabled string interning?
    Sunday, January 16, 2011 9:30 PM
  • Hi,

      I think there are something else  using it when removed one object ,though it looks being released.When the dictionary is empty,the objects are really released.Furthermore,the type of string has a issue that it can lingering in the memory.You can try the other types.

    Tuesday, January 18, 2011 2:57 AM
  • Hi Bahgat,

     

    Based on your description, it’s a problem related to CLR forum. This forum is dedicated to C# technology. I'm moving this thread from Base "Visual C# General" to “CLR " forum, since this issue is related to CLR.There are more experts in that forum.

     

    Thank you for your posting and understanding.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 20, 2011 7:40 AM