none
Is it required to clear Collection while disposing? RRS feed

  • Question

  • Hi,

        A class having collection of some object as member, do i need to clear the collection in Dispose()?

    class Class1
    {
       string a;
       object b;
       int c;
    }
    
    class MyClass : IDisposable
    {
        List<Class1> someList;
        
        public MyClass()
        {
          someList = new List<Class1>();
        }
    
        private UpdateList()
        {
          //Update List
        }
       
        public void Dispose()
        {
           //Do i need to clear the list?
         }
    
    }

    Thanks

    Thursday, September 6, 2012 6:05 AM

Answers

  • In general no, you do not need to clear the list in Dispose. In general Dispose is intended for resources that are not under the control of GC: files, sockets, etc.

    The only case where clearing that list makes sense is if you know that a disposed instance of MyClass will be kept alive for a long time after calling Dispose. But such a scenario is rare and usually indicates a bug, once you dispose something it means you no longer need it and you should not keep references to it.

    I assume that this is just an example because as it is there's no reason to event implement IDisposable in this class.

    Thursday, September 6, 2012 6:15 AM
    Moderator
  • Events are a bit more interesting, they don't fall into the "file, socket etc." category but they're a common source of memory problems. Still, removing the events is not the job of the class that declares the event, it's the job of event subscribers and your class should not do anything about it in Dispose.

    In contrast, if your class subscribes to an event of another class then it should remove the event handler in Dispose (unless it knows for certain this is not added or it has other options).

    Thursday, September 6, 2012 1:12 PM
    Moderator

All replies

  • In general no, you do not need to clear the list in Dispose. In general Dispose is intended for resources that are not under the control of GC: files, sockets, etc.

    The only case where clearing that list makes sense is if you know that a disposed instance of MyClass will be kept alive for a long time after calling Dispose. But such a scenario is rare and usually indicates a bug, once you dispose something it means you no longer need it and you should not keep references to it.

    I assume that this is just an example because as it is there's no reason to event implement IDisposable in this class.

    Thursday, September 6, 2012 6:15 AM
    Moderator
  • Hello, IMO if objects in list<> implements IDisposable then you can loop through the List and dispose each object in list.

    If it does not implement IDisposable you can clear the list or may it null. As no reference exist GC will clear the memory. 

       public void Dispose()
        {
    // If IDisposable is not implemented
    somelist.clear()  //or someList = null
         }

    HTH


    please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.

    Thursday, September 6, 2012 6:20 AM
  • As stated by others, if the items in the list are IDisposable, it's beneficial to dispose of them. 

    However if you read the documentation for List<>.Clear, it states that the capacity remains unchanged.  So there's little to gain by calling it.  I'd say it's better to let the GC do it's work and have one less line of code to maintain.


    This signature unintentionally left blank.

    Thursday, September 6, 2012 9:18 AM
  • Thanks Mike,

    If i have event as member variable then do i need to clear invocation list?

    Or will this also be taken care by GC.

    Thanks.

    Thursday, September 6, 2012 10:18 AM
  • Events are a bit more interesting, they don't fall into the "file, socket etc." category but they're a common source of memory problems. Still, removing the events is not the job of the class that declares the event, it's the job of event subscribers and your class should not do anything about it in Dispose.

    In contrast, if your class subscribes to an event of another class then it should remove the event handler in Dispose (unless it knows for certain this is not added or it has other options).

    Thursday, September 6, 2012 1:12 PM
    Moderator
  • Thanks Mike Danes, that helped a lot!!!!
    Saturday, September 8, 2012 4:06 PM