Consulting

Results 1 to 2 of 2

Thread: Memory Leak when Sending Many Messages

  1. #1

    Memory Leak when Sending Many Messages

    Hi Everyone,

    I have some VBA code that sends out all the emails that have been saved into a particular folder when Outlook launches. The emails are saved there by another application using .NET Office Interop.

    The problem is that I have memory leak. Each time Outlook calls MailItem.Send, I see the Mem Usage in Task Manager for Outlook increase by about 2MB. So Outlook slowly balloons from about 25MB to 116MB, at which point the VBA code crashes with a generic error "Method 'Send' in 'MailItem' failed."

    I have tried every possible manner trying to induce garbage collection on the items that have been sent (mostly different variations of setting my MailItem instances to Nothing). I have also tried moving the sending code into its own Sub, in the hopes that the garabage collector would then release the MailItems as they are sent. Nothing has worked.

    Here is my current code, but this is version 125.52345 of all the different things I have tried to stop the memory leak.

     
    Private Sub Application_Startup()
      DoEvents
     
      Dim ns As NameSpace
      Dim drafts As MAPIFolder
      Dim rmsDrafts As MAPIFolder
     
      Set ns = GetNamespace("MAPI")
      Set drafts = ns.GetDefaultFolder(olFolderDrafts)
      Set rmsDrafts = drafts.Folders("rms")
     
      Do While rmsDrafts.Items.count > 0
        Call sendItem(rmsDrafts)      
        DoEvents
      Loop   
     
    End Sub
     
    Private Sub sendItem(rmsDrafts As MAPIFolder)
      Dim oItem As MailItem
      Set oItem = rmsDrafts.Items(1)
      oItem.Send
      Set oItem = Nothing
    End Sub
    Basically, what I need is for memory to be freed up as each item is sent.

    I'm sure there is some simple solution, but I have found any definitely answer to the whole memory leak / garbage collection debate - and certainly nothing that seems to work for my particular situation.

    Any feedback would be greatly appreciated! Thank you!

  2. #2
    Sorry, forgot to mention that this is Outlook 2003.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •