none
Memory Leake in Excel after COM AddIn Unload RRS feed

  • Question

  • I have created a COM AddIn for Excel using VSTO, I'm unloading AddIn but somehow excel process is not releasing  memory occupied during AddIn initialization.

    Calling Dispose() function on .NET object then making it null, then calling GC.Collect(), GC.WaitForPendingFinalizers() but it is releasing small amount of memory for that particular object but not for common memory consumed by Excel process during AddIn initialization, even I've used following options.

    Marshal.FinalReleaseComObject(worksheet);

    Marshal.FinalReleaseComObject(Globals.ThisAddIn.Application.ActiveWorkbook);

    Even if I close workbook but memory associated with excel is not releasing. It appears that Excel process need to flush memory but not doing.

    Any suggestion is welcome!!


    MS Technologies Developer


    • Edited by madhur Tuesday, September 9, 2014 5:37 PM
    Tuesday, September 9, 2014 5:35 PM

Answers

  • Thanks Dmitry!! It appears that Third Party Lightning WPF Charts are leaking memory even though you release chart object but addin is not releasing, contacted Lightning Chart team and they assured to look into this.

    MS Technologies Developer

    Saturday, September 13, 2014 12:31 PM

All replies

  • Office applications use internal memory allocators, so if you release an Object Model object, do not expect the allocated memory in Windows Task Manager to go down. Plus if the worksheet is still open (even if you are not referencing it), the memory would still be allocated by internal Excel objects.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, September 9, 2014 6:26 PM
  • Thanks Dmitry!!

    when I perform some operations on excel sheet (provided by Add-in) then after 4-5 clicks it throws out of memory exception, it hides and shows .NET charts. So it allocates memory on each initialization of Add-In instance but doesn't seem to release except released by my add-in itself. 


    MS Technologies Developer

    Tuesday, September 9, 2014 6:41 PM
  • Make sure that you release all underlying COM objects in the code. It seems not all objects are released instantly. See Systematically Releasing Objects. The article is related to Outlook, but the same rules can be applied to other Office applications.

    Or, as a workaround, you need to use the Collect and WaitForPendingFinalizers methods of the GC (garbage collector) class.  The way in which .NET objects are stored in memory requires you to run the GC twice:

    GC.Collect
    GC.WaitForPendingFinalizers
    GC.Collect
    GC.WaitForPendingFinalizers

    Does it help?

    If not, are you able to reproduce the issue with a small piece of code? So, I could test it on my machine and find a suitable solution.

    Tuesday, September 9, 2014 7:21 PM
  • Are you using multiple dot notation anywhere?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, September 9, 2014 8:14 PM
  • thanks Eugene!!

    I have tried with GC.Collect, GC.WaitForPendingFinalizers , GC.Collect,GC.WaitForPendingFinalizers but no impact.


    MS Technologies Developer

    Wednesday, September 10, 2014 2:23 PM
  • Thanks Dmitry!! If "multiple dot notation" means nested objects on worksheets then yes e.g. vstoSheet.Range["CT3"].Offset[0,axisXDivIndex].Value


    MS Technologies Developer

    Wednesday, September 10, 2014 2:27 PM
  • Try to break such statements into separate variables and release them using Marshal.ReleaseComObject.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, September 10, 2014 2:34 PM
  • Thanks Dmitry!! It appears that Third Party Lightning WPF Charts are leaking memory even though you release chart object but addin is not releasing, contacted Lightning Chart team and they assured to look into this.

    MS Technologies Developer

    Saturday, September 13, 2014 12:31 PM