locked
C# WinForms Application + Interop.Word - Severe Memory Usage Issues. RRS feed

  • Question

  • [ I hope this is the correct forum for this question. I did some searching and looking around for similar issues before, This seems to be the right forum for this. Sorry in advance if im mistaken.]

     

    Hi, Everyone.
    I'm having a slight problem with virtual memory usage in Word's process when using the object model,
    through the .NET Interop. I'm running Office 2007, Working in a .NET 2.0 environment.
    The solution is a normal WinForms project.

     

    What i'm doing basically, is loading a bunch of data rows from an SQL Server DB using the native SqlClient namespace classes.
    Those rows contains RTF strings in them, And i'm using word to convert those to HTML.
    I do that by writing the RTF string of the row im processing to a file, loading an empty RTF document in Word,
    and Inserting that file into the document using Word.Application.Selection.InsertFromFile.
    I then save the document as HTML, and read the file's contents again to retrieve the HTML result.

     

    I have quite a large number of rows to process (8 tables, each table contains at least 50,000 rows, Some more than double that figure),
    And after about an hour of work (around 90,000 rows processed), Word's VM usage reachs over 2GB and Word starts complaining about lack of free space.
    The phsyical HDD has over 10GB of free space, and i still have around 400MB of RAM free (out of a total 1GB), So i'm not sure if its actually a resources issue.
    The Commit chrage on that system (a workstation, not the DB server itself) starts at around 300-500M and grows to around 2.4GB,
    when Word process is reponsible for at least 2GB from that, In virtual memory usage.

     

    Im pasting the relevant code using Word's Interop in hope someone spots something im doing to cause
    such a huge raise in Word's VM usage.

     

    Note: oTrue, oFalse and oMissing are not defined since they are obviously what they mean - object boxing for the true, false, and Missing.Value values.
    Also, The other oObjects are all boxing of the respective Word enumration values. Their names should give away their meaning, But if someone wants me to,
    I'll paste their definition as well.
    Paths were short-handed to the root drive for pasting purposes only, And they are usually different & longer.

     

    Code Snippet

    ...
    // Data Members
    private Microsoft.Office.Interop.Word.Application wordApplication;
    private Microsoft.Office.Interop.Word.Document wordDocument;
    ...
    // The WinForm's Load event
    ...
    this.wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
    ...
    // The main operation using the Interop, Running in a BackgroundWorker.Do_Work method.
    // I tried even without the worker, The secondary thread wasn't the issue, From what i've seen.
    object sourceFile = @"c:\source.rtf";
    this.wordDocument = this.wordApplication.Document.Open(ref File, ref oFalse, ref oFalse, ref oFalse, ref oMissing, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oFormatRTF, ref oMissing, ref oFalse, ref oMissing, ref oDirectioRTL, ref oMissing, ref oMissing);
    string tempFileName = @"c:\temp.rtf";
    File.WriteAllText(tempFileName, rtfString); // rtfString is the value read from a given dataRow. contains a full rtf string.
    this.wordDocument.Select();
    this.wordApplication.Selection.Text = "";
    this.wordApplication.Selection.InsertFromFile(tempFileName, ref oMissing, ref oFalse, ref oMissing, ref oMissing);
    object saveFile = (object)tempFileName;
    this.wordDocument.SaveAs(ref oFalse, ref oMissing, ref oMissing);
    this.wordDocument.Close(ref oFalse, ref oMissing, ref oMissing);
    ...

     

     

    Tuesday, August 28, 2007 7:20 AM

Answers

  • I had similar problems automating Word in different environments (pre 2007 / VBA). I think it’s a good idea to reduce the amount of work you do within one instance of the com object. You could use Quit on the Word object and get a new one every 100 -1000 records. Though not a solution it could be a workaround.

    Tuesday, August 28, 2007 8:09 AM

All replies

  • I had similar problems automating Word in different environments (pre 2007 / VBA). I think it’s a good idea to reduce the amount of work you do within one instance of the com object. You could use Quit on the Word object and get a new one every 100 -1000 records. Though not a solution it could be a workaround.

    Tuesday, August 28, 2007 8:09 AM
  •  

    Yeah, I've thought of something similar. I've put in a 'hack' to quit and get a new ApplicationClass Instance every 50,000 row or so, But i guess that value can be even lower.
    Tuesday, August 28, 2007 10:23 AM