Click here to Skip to main content
Click here to Skip to main content

Office Interop Object Collection Technique – Ghost Excel Instance

, 4 Jan 2010
Rate this:
Please Sign up or sign in to vote.
Office Interop Object Collection Technique – Ghost Excel Instance

Recently, we wrote an add-in for Microsoft PowerPoint to automate several routine tasks for our business users. While developing this add-in, we required to open a temp workbook in a new Excel application instance to perform several tasks. Once these tasks were completed, the temp workbook was supposed to be closed and the Excel application needed to be quit. However to our amazement, we found that in spite of quitting Excel application by calling Quit() on Excel application object, we found it to be silently running in task manager. It seemed that this process lives in memory forever until the AppDomain on which add-in is loaded is torn.

After this, we researched a bit to see what is really causing the Excel process to live in memory forever. And to answer this and several other basic questions, I wrote a small windows console application as my POC. Below is how the source appears.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Excel = Microsoft.Office.Interop.Excel; 
using System.Reflection; 
using System.Runtime.InteropServices;
namespace GhostXLInstance 
{ 
  class Program 
  {
        static void Main(string[] args)
        {
            Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Excel.Workbook newWorkbook = xlApp.Workbooks.Add(Missing.Value);
            int workbookRCWCounter = Marshal.ReleaseComObject(newWorkbook);
            Console.WriteLine("RCW counter for workbook :: 
		" + workbookRCWCounter.ToString());
            xlApp.Quit();
            int excelRCWCounter = Marshal.ReleaseComObject(xlApp);
            Console.WriteLine("RCW counter for XL application :: 
				" + excelRCWCounter.ToString());
            Console.Read();
        }
    }
}

In the above code, I am creating a new Excel application, adding new workbook in it and then releasing both application and workbook references (observe that we explicitly created only two interop references xlApp and newWorkbook).

After executing the example, you will see two lines in the console telling you the RCW count for each object we released. Viola!!! It is zero which apparently means that all the objects we referenced are released. However, if you observe the task manager now, the Excel instance is still running. So the question still remains the same – What is holding the Excel process?

Well, then I got a fantastic article http://blogs.msdn.com/geoffda/archive/2007/08/31/the-designer-process-that-would-not-terminate.aspx which explained the phenomena. So here is the phenomenon in simple words. If you closely observe our example, we are actually getting three references. The third reference is implicitly created when we used the application’s Workbooks collection to add new workbook.

Excel.Workbook newWorkbook = xlApp.Workbooks.Add(Missing.Value);

Observe that xlApp.Workbooks actually returns Microsoft.Office.Interop.Excel.Workbooks reference. Wow! So did we get the cause of immutability of Excel process? Yes! So this uninvited third reference lives forever in the memory and causes the Excel process to wait for its garbage collection.

Now since we got the cause, let's modify our previous code to release third reference even.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Excel = Microsoft.Office.Interop.Excel; 
using System.Reflection; 
using System.Runtime.InteropServices; 
namespace GhostXLInstance 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); 
            Excel.Workbooks workbooks = xlApp.Workbooks; 
            Excel.Workbook newWorkbook = workbooks.Add(Missing.Value);            
            int workbookRCWCounter = Marshal.ReleaseComObject(newWorkbook); 
            Console.WriteLine("RCW counter for workbook :: 
				" + workbookRCWCounter.ToString()); 
            int workbooksRCWCounter = Marshal.ReleaseComObject(workbooks); 
            Console.WriteLine("RCW counter for workbooks :: 
				" + workbooksRCWCounter.ToString()); 
            xlApp.Quit(); 
            int excelRCWCounter = Marshal.ReleaseComObject(xlApp); 
            Console.WriteLine("RCW counter for XL application :: 
				" + excelRCWCounter.ToString()); 
            Console.Read(); 
        } 
    } 
}

Finally in modified code, I am collecting the xlApp.Workbooks reference to a local variable and then releasing it. And now executing this code would release the ghost application while console application is waiting at Console.Read().

Conclusion

Well so from our POC it became apparent that the so called collection references (like workbooks, shapes, charts, …) need to be explicitly referenced in local variable and garbage collected to neatly exit Excel process.

Note: In my next posting, I will detail the right way to release Interop references.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Samir Sameja
Program Manager
India India
No Biography provided

Comments and Discussions

 
Generalhelpful PinmemberSergey Stoyan2-Sep-10 7:46 
GeneralMy vote of 5 PinmemberSergey Stoyan2-Sep-10 7:46 
Generalit's very good PinmemberVinentWang17-Aug-10 2:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 4 Jan 2010
Article Copyright 2010 by Samir Sameja
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid