I use for my MVC application a parser for a word document. I noticed that after the parser runs, the document remains in memory. Used method:

Marshal.ReleaseComObject(Doc); Marshal.ReleaseComObject(MSWord); 

but it does not help, the object is still hanging in memory. How to delete an object of type System.__COMObject from memory?

Problem lines:

 Word.Application MSWord = new Word.Application(); Word.Document Doc = MSWord.Documents.Open(urlDocMenu, ConfirmConversions: true); 

After these lines an object appears in memory and I do not know how to remove it.

  • Can you provide a minimal example that reproduces the problem? (And yes, how do you know that the object is still alive?) - VladD
  • @VladD Yes, sorry, a description of the problem of the rules. And I found out about the object by going to the Task Manager trivially, seeing 4 Vordov processes there - Alex
  • And, it becomes clearer. And if you do the same Marshal.ReleaseComObject and wait without doing anything? I do not remember how many, a minute or five, so that the COM server is dead. - VladD

3 answers 3

In my opinion, the code could be something like this:

 var wordApp = new Microsoft.Office.Interop.Word.Application(); wordApp.Visible = true; var doc = wordApp.Documents.Open(urlDocMenu); ... doc.Close(); wordApp.Quit(); 
  • one
    That's right, to close Word, use the Quit method. I didn't have time to answer for one minute =) - Alexis
  • one
    everything ingenious is simple =) sorry for flooding - cvvvlad

According to the information from msdn Marshal.FinalReleaseComObject(Val) guaranteed to remove all links and allow the object to close.

    In my app.Quit() works correctly, but you can also try to set the value app = null then call the garbage collector GC.Collect() .

    • one
      Not. You cannot hope that the garbage collector will clean up after you, GC.Collect() does not guarantee anything. In addition, it is a waste of resources: the garbage collector needs a lot of work to find the only link that you release. In addition, it knocks down the auto-tuning of the garbage collector and degrades program performance. And all this - so as not to call Quit once? - VladD