Geeks With Blogs
New Things I Learned

I learned something new, relating to when/how to dispose CollectionView, but it comes after a series of steps of proving it.  Along the way I learned some new stuff to, so I'll stagger them so it becomes easier to read & digest.  This post will become the first one in this series (of 3 I think).

I'm always somewhat wary about the automatic garbage collection nature of .NET, so every now and then I need to prove to myself that something is actually garbage collected.  The technique I use is to create a Finalizer in my class that will signify that that object's finalize is being called (which means it is being garbage collected).  If the finalizer doesn't get hit, then the object doesn't get garbage collected.

Single objects are fine to debug - you put a breakpoint in your finalizer and be done with.  But there are times I wanted to verify that multiple objects are garbage collected - there are also times when I wanted to know what objects are not yet garbage collected, and what has.  So, I developed the following mechanism.

public static class ItemCollector

{

   private static int _counter = 0;

   // This function creates an Id String for the object being created

   public static string GetStringForItemCreation(Type itemType)

   {

      string id = string.Format("{0} number {1}", itemType.FullName, ++_counter);

      _itemsCreated.Add(id);

      return id;

   }

 

   // This function will add the id to the itemsDestroyed collection

   public static void ReturnStringForItemDestruction(string id)

   {

      _itemsDestroyed.Add(id);

   }

 

   private static ThreadAwareObservableCollection<String> _itemsCreated = new ThreadAwareObservableCollection<string>();

   // String collection of all item id created

   public static ThreadAwareObservableCollection<String> ItemsCreated

   {

      get { return _itemsCreated; }

   }

 

   private static ThreadAwareObservableCollection<string> _itemsDestroyed = new ThreadAwareObservableCollection<string>();

   // String collection of all item id destroyed

   public static ThreadAwareObservableCollection<string> ItemsDestroyed

   {

      get { return _itemsDestroyed; }

   }

}

This static class has 2 string collection - I'm using the thread-aware collection since garbage collection will be called by a different thread - one to collect all string for items that are created, and another one to collect string for item that is garbage collected.  The GetStringForItemCreation will return a string giving a unique Id for that item and add it to the first collection - the ReturnStringForItemDestruction will just add the given Id to the second collection.

These functions will be called by my objects in their construction and destruction - for instance I'd like to see when ListCollectionViews are garbage collected, so I create a class deriving from it as follows:

public class ListCollectionViewEx : ListCollectionView

{

   public ListCollectionViewEx(System.Collections.IList list)

      : base(list)

   {

   }

 

   ~ListCollectionViewEx()

   {

      ItemCollector.ReturnStringForItemDestruction(_creationId);

   }

 

   private string _creationId = ItemCollector.GetStringForItemCreation(typeof(ListCollectionViewEx));

}

Now, whenever this class is created, it will get a string Id (and add it to the main collection), and when the object is garbage collected, the finalizer will return the string id to our ItemCollector class.  You can modify the ItemCollector class so it also puts a timestamp, removes the id from ItemsCreated collection, etc. - but the basic idea is the same: On object construction you'd like to register an object has been constructed, and on destruction you want to note that the object has been destructed.  By having this information, you can then see which objects are still not garbage collected, and which objects have been.

Please note that you only want to put finalizers for debugging purposes - having a finalizer will impact performance (and garbage collection) of your objects.  Once you verify the item is being garbage collected, then you can take them out.  I thought about using [Conditional("DEBUG")] but that attribute doesn't work on constructors/destructors.

A small test code I made then is as follows:

for (int i = 0; i < 100; i++)
{
   ListCollectionViewEx lcv = new ListCollectionViewEx(ItemCollector.ItemsCreated);
}
 
GC.Collect();

Pretty simple - you can use this just to prove that your objects are being garbage collected. Calling GC.Collect will force garbage collection, thus since the ListCollectionViewEx objects are not collected / referenced, all those objects should be garbage collected.  You can use this construct to test your classes.  The only kicker is that the above code will result in no items / Ids being added to our ItemsDestroyed list.  Why is that?  That's for tomorrow's blog - I have to pace myself you know .

Posted on Thursday, January 17, 2008 2:10 PM .NET | Back to top


Comments on this post: Verifying objects do get Garbage Collected

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Muljadi Budiman | Powered by: GeeksWithBlogs.net