none
Garbage Collection and ReleaseCOMObject RRS feed

  • Question

  • I was updating some old code and noticed a pattern that I was following (see code example below). 

    Do I really need to be calling those Garbage collection methods?  The nature of my Add-In is that users are creating/editing/deleting appointments throughout the day.   All those GC calls can't be good.  

    My guess is the calls to GC.Collect and GC.WaitForPendingFinalizers aren't needed because the Garbage collector will take care of those released RCWs by itself at some point.   I'm not storing a large amount of unmanaged objects that suddenly need releasing.


    void InspectorActivate( object inspector )
    {
       Outlook.Inspector Inspector = inspector;
       Object item = Inspector.CurrentItem;
    
       if( item is Outlook.AppointmentItem )
       {
           //do stuff
       }
    
       if( item != null){ Marshal.ReleaseComObject(item); item=null; }
       GC.Collect();
       GC.WaitForPendingFinalizers();
    
    }
    


    Germán Hayles

    Thursday, May 23, 2019 6:16 PM

Answers

  • If you remove them then if you encounter strange results from the software that you cannot figure out then you must include in your question that you removed some code from the software that was working but you wanted to fix the code anyway. Or you can just not fix it because it is not broken.

    .Net is unable to do automatic garbage collection on COM objects so it is possible that the previous developer learned that they must do the explicit cleanup. They might have over-reacted and done some garbage collection in places that it was unneeded but until you understand better how things work you should leave it as-is.



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by GermanEZI Tuesday, May 28, 2019 8:38 PM
    Friday, May 24, 2019 5:48 PM

All replies

  • If you remove them then if you encounter strange results from the software that you cannot figure out then you must include in your question that you removed some code from the software that was working but you wanted to fix the code anyway. Or you can just not fix it because it is not broken.

    .Net is unable to do automatic garbage collection on COM objects so it is possible that the previous developer learned that they must do the explicit cleanup. They might have over-reacted and done some garbage collection in places that it was unneeded but until you understand better how things work you should leave it as-is.



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by GermanEZI Tuesday, May 28, 2019 8:38 PM
    Friday, May 24, 2019 5:48 PM
  • Thanks Sam.

    I did some digging around and I saw an example that actually did something like

    GC.Collect()
    // Released COM objects
    GC.Collection()
    GC.WaitForPendingFinalizers()

    Possibly in earlier releases of Office (e.g. 2007, 2010) we needed to do more, but everything I'm reading dealing with Office 2013 and above really frowns upon the forced Garbage Collection unless you've amassed a rather large number of objects in which case it's not a bad idea.


    Germán Hayles

    Tuesday, May 28, 2019 8:37 PM