Out of memory exception when dragging and converting over 500 items at once

If I drag and drop over 500 items at once from outlook (I am trying to drag a few thousand) I get an out of memory exception when it is looping through (and gets to about 400) even though my code seems to tidy everything up.

I have done this async using a worker thread but the same happens when I use it on a single thread.

I also have used every thing in memory but again saving as files causes the same issues.

The garbage collector is tidying it’s self up but bizarrely it still happens. I am aware of pinning (Yun Jin's WebLog | Microsoft Learn) in the stack but to my knowledge this happens mainly with file access during threading.

I have also called GC.Collect (event though I should not do need to) but this does not help as the gc seems to be working.

Do you use unmanaged code in your component that I need to tidy up that is not done by the dispose of the message object?

Thank you


Private Sub LoadMessages(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles _loadMessagesWorker.DoWork

Dim args As Aspose.Windows.Forms.FileDragEventArgs
args = DirectCast(e.Argument, Aspose.Windows.Forms.FileDragEventArgs)

Dim total As Integer = args.Files.Count
_messagesToProcessCount = 0

_loadMessagesWorker.ReportProgress(0, String.Format(“Importing from Outlook {0} of {1}”, 1, total.ToString(“###,###,###”)))

Dim msg As Aspose.Network.Mail.MailMessage = Nothing
Dim msgFs As System.IO.MemoryStream = Nothing
Dim emlFs As System.IO.MemoryStream = Nothing

For i As Integer = 0 To total - 1

Try
If _loadMessagesWorker.CancellationPending Then Return

'save as msg
msgFs = New System.IO.MemoryStream
args.Files(i).Save(msgFs)

If _loadMessagesWorker.CancellationPending Then Return

'Reset Memorystream
msgFs.Seek(0, IO.SeekOrigin.Begin)
msg = Aspose.Network.Mail.MailMessage.Load(msgFs, Aspose.Network.Mail.MessageFormat.Msg)

If _loadMessagesWorker.CancellationPending Then Return

'save to eml file
emlFs = New System.IO.MemoryStream
msg.Save(emlFs, Aspose.Network.Mail.MessageFormat.Eml)

'my code to process the file but error event if I remove it
ProcessMessage(emlFs)
_messagesToProcessCount += 1

_loadMessagesWorker.ReportProgress(CInt((i + 1) / total * 100), String.Format(“Importing from Outlook {0} of {1}”, (i + 1).ToString(“###,###,###”), total.ToString(“###,###,###”)))

Debug.Write(“Current” & i.ToString & “:”)
Debug.WriteLine(GC.GetTotalMemory(True))

Catch ex As Exception
MessageBox.Show(“Save Failed:” & ex.Message)
Finally
If msgFs IsNot Nothing Then msgFs.Close()
If emlFs IsNot Nothing Then emlFs.Close()
If msg IsNot Nothing Then msg.Dispose()

msg = Nothing
msgFs = Nothing
emlFs = Nothing
End Try

If _loadMessagesWorker.CancellationPending Then Return
Next

MessageBox.Show(“Complete”)

End Sub

Looking into the issue further the following code on a standard windows form causes the same error after about 400 once the cursor is moved back over the form.

It may be that I am wrting it wrong i am not sure?

This is quite urgent.

Thank you

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim msg As Aspose.Network.Mail.MailMessage = Nothing


For i As Integer = 0 To 2000
Try
msg = Aspose.Network.Mail.MailMessage.Load(“C:\myMessage.msg”, Aspose.Network.Mail.MessageFormat.Msg)
Debug.WriteLine(i.ToString & " : " & GC.GetTotalMemory(False))
Finally
If msg IsNot Nothing Then msg.Dispose()
End Try
Next

MessageBox.Show(“Complete”)
End Sub

Dear JK_Online,

Thank you for your post!

Our developers are working at this issue and trying to reproduce and trace the problem, we'll back to you ASAP.

Thanks

Is there any update can you reproduce the issues?

Thank you

Hello,

Because the component extracts the file content to memory first, if too many items are dragged and dropped, the system may run out of memory.

It is the limitation on current design -- if we do not use temp file.

Our developers are still working on it to find other solution if any. Are you sure your scenario need to drag and drop so many mails at the same time?

Thanks

Draging this many is not that important however even if I process a folder with this many in the same thing happens or if i do one batch then another then another.

To me the code below should work fine even if it is all in memory as the dispose should clean up any memory used (or mark it for GC) and allow me to continue.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim msg As Aspose.Network.Mail.MailMessage = Nothing


For i As Integer = 0 To 2000
Try
msg = Aspose.Network.Mail.MailMessage.Load(“C:\myMessage.msg”, Aspose.Network.Mail.MessageFormat.Msg)
Debug.WriteLine(i.ToString & " : " & GC.GetTotalMemory(False))
Finally
If msg IsNot Nothing Then msg.Dispose()
End Try
Next

MessageBox.Show(“Complete”)
End Sub

Thanks for your updates.

We will have some memory improvement in the coming release. We revised some code, in order to reduce the momory allocations.

However, as I mentioned previously, the file is saved in memory instead of temp file. So the memory will be large if dragging and dropping to many items into the form.

Overall, I think your solution is good. If we change the design and use temp file, I think it is not a good idea. It will introduce more other issues, like security, configuration.