none
Excel not unloading from memory (interop) RRS feed

  • Question

  • Hello,

    I have a WPF app that opens excel, populates some worksheets and creates some charts for the user.

    I am using Microsoft.Office.Interop and Microsoft.Office.Interop.Excel

    The problem is, when the user saves and quits excel it stays hidden in memory. I have read other articles about this problem but they all involve programmatically closing excel (and callingSystem.Runtime.InteropServices.Marshal.ReleaseComObject)

     which I don't want to do. I want the user to be able to interact with the new spreadsheet and close it when they are ready.

    Thanks in advance!

    Cory

    Tuesday, September 26, 2017 6:06 PM

All replies

  • Hi Cory,

    What do you mean the excel stays hidden in memory? Could you see the excel is still in task manager?

    I tried below code to open a workbook and it disappears from task manager after saving and quitting the workbook.

    string path = @"C:\Users\Administrator\Desktop\Test.xlsx";
    
                Excel.Application xlApp = new Excel.Application();
    
                xlApp.Visible = true;
    
                xlApp.Workbooks.Open(path);

    What's the version of your Office and could you reproduce this issue on other machine?

    Besides, could you tell us your code and test steps so that we could try to reproduce your issue?

    Best Regards,

    Terry


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 27, 2017 6:16 AM
  • Hi Cory,

    There are some tricks to doing COM interop safely from .NET. You never have to do manual reference management (Marshal.ReleaseComObject) but you should note the following:

    * After all your .NET references are released (set to null or out of scope) you should run GC.Collect() before closing your app. This is much easier if your references are local variables or passed around as parameters.

    * You have to be careful when testing things in the debugger - it can keep local variables alive longer then you expect, preventing the GC from releasing the COM objects.

    For details and a little example that should work cleanly (the Excel process closes) from which you can start, check my detailed answer on StackOverflow: https://stackoverflow.com/questions/15728676/proper-way-of-releasing-com-objects/38111137#38111137

    -Govert

    Excel-DNA - Free and easy .NET for Excel

    Wednesday, September 27, 2017 8:02 PM