Outlook 2007 add-in memory leak?

I created a simple Outlook 2007 add-in using C #, which iterates over a list of messages and analyzes their attachments.

I use this add-in to set out ~ 25,000 selected posts. Immediately, however, I notice the use of Outlook memory (visible through perfmon) firing. After starting the add-in in debug mode, it is obvious that the memory is assigned to Outlook in the first instance of access to the Message Attachments collection. This memory never returns to the system; Outlook continues to consume memory up to 1 GB (after about 12,000 messages), after which I get an error "from memory or system resources." Any ideas?

Here is the piece of code:

        for(int i = 1; i <= objSelectedItems.Count; i++)
        {
            Object objMsg = objSelectedItems[i];

            //Only process if Item is a Message
            if (objMsg is Outlook.MailItem)
            {
                Outlook.MailItem Msg = objMsg as Outlook.MailItem;

                //The culprit: this allocates memory to Outlook which I can't get back
                Outlook.Attachments objAttachments = Msg.Attachments;

                //Perform some actual work here//

                //Clean up Outlook objects; does not appear to give memory back to system
                Msg.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);
                Marshal.ReleaseComObject(objAttachments);
                Marshal.ReleaseComObject(Msg);
            }

            Marshal.ReleaseComObject(objMsg);
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }            
+3
4

, . objSelectedItems Applicaiton.ActiveExplorer(). , :

  • objSelectedItems .
  • Marshal.ReleaseComObject(objSelectedItems).
  • , , Outlook.

, -, , - , .

0

foreach ( )?

foreach , for :

OOM.NET: 2 - Outlook

-, , , .

, ?

for (int i = 1; i <= oAttachs.Count; i++)
{
    Outlook.Attachment oAttach = oAttachs[i];

    // Do nothing with attachment
    Marshal.ReleaseCOMObject(oAttach);
    oAttach = null;
}
+2

Msg , :

 Outlook.Attachments objAttachments = Msg.Attachments;

, ... 5000 , 5000 ~ 25 000

0

, Marshal.ReleaseComObject() 0, , -?

, Dispose. Dispose()

0

Source: https://habr.com/ru/post/1720199/


All Articles