We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

System.OutOfMemory exception / memory leak?


i’m getting a system.outofmemory-exception when loading multiple outlook messages with attachments (msg-files) using Aspose.Email.NET-API.

Here’s a simple example code to reproduce the behaviour:

int iVar;
Aspose.Email.MailMessage mMessage = null;

for (iVar = 0; iVar < 1000; iVar++)
		mMessage = MailMessage.Load(@"c:\test.msg", new MsgLoadOptions());
		if (mMessage != null)
			mMessage = null;

You can see a fast growing memory usage of the app in the task manager, and after some time (depending on the attachment size) the exception comes.

I’m using version of the Aspose.Email-assembly.

Is this a known issue? Or do i have to call any other method to dispose the MailMessage-object?

Thank you and best regards


MailMessage.Dispose() is the right method to dispose loaded message object. We tried sample messages at our end using the latest version of the API but were not able to face any such exception. Please share your sample messages with us for further investigation at our end.

Thank you for your answer.

I’ve uploaded a sample project (Visual Studio 2010) and a sample msg-file (Aspose-newsletter with added aspose.email.xml-help file).
The exception occurs at approximately 1,5 Gigs of memory usage. You can see the fast usage growth in the windows task manager.

Samples: pAMailAPITest.zip (691.6 KB)


We have downloaded and executed your project at our end and it runs to completion without any Out of Memory issue. Could you please share your environment details with us? Specifically, we are interested to know about the OS (32-bit or 64-bit) and the memory installed at your end. It will help us investigate the issue further in detail and assist you accordingly.

I’m using Windows 7 Enterprise, 64 Bit, Intel Core i7 with 16 GB RAM and Visual Studio 2010 Ultimate.

Here are some more details of the exception thrown, perhaps it helps (OutOfMemoryException is in the inner exception):

Aspose.Email.FileCorrupedException {“Compound File Binary Format corrupted”}
bei Aspose.Email.Tools.FileFormatUtil.#=qfY5J8vDtLy_PS1vxkaSbCw==(Stream #=qTqQaf7kpzZcHQSEyDnB0yg==)
bei Aspose.Email.Mapi.MapiMessage.IsMsgFormat(Stream stream)
bei Aspose.Email.MailMessage.#=qB7fAs7gKvUX58Av_nAFSng==(Stream #=qTqQaf7kpzZcHQSEyDnB0yg==, LoadOptions& #=qHgmybkduHS8HPZm$NSK97w==)
bei Aspose.Email.MailMessage.#=qCCix0gOEp4itbZbwWBer0SPhjBx$E_RX48IObE4uZ94=(Stream #=qTqQaf7kpzZcHQSEyDnB0yg==, LoadOptions #=qHgmybkduHS8HPZm$NSK97w==)
bei Aspose.Email.MailMessage.Load(String fileName, LoadOptions options)
bei pAMailAPITest.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\dluetke\Documents\Visual Studio 2010\Projects\pAMailAPITest\Form1.cs:Zeile 29.
bei System.Windows.Forms.Control.OnClick(EventArgs e)
bei System.Windows.Forms.Button.OnClick(EventArgs e)
bei System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
bei System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
bei System.Windows.Forms.Control.WndProc(Message& m)
bei System.Windows.Forms.ButtonBase.WndProc(Message& m)
bei System.Windows.Forms.Button.WndProc(Message& m)
bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
bei System.Windows.Forms.Application.Run(Form mainForm)
bei pAMailAPITest.Program.Main() in C:\Users\dluetke\Documents\Visual Studio 2010\Projects\pAMailAPITest\Program.cs:Zeile 18.
bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
bei System.Threading.ThreadHelper.ThreadStart()

Inner exception:
System.OutOfMemoryException {“Eine Ausnahme vom Typ “System.OutOfMemoryException” wurde ausgelöst.”}

bei System.IO.MemoryStream…ctor(Int32 capacity)
bei #=q4KDzEOexdf9g9V0IF3u90ebtBbwQ8W$TQVFCcH8WL6OruEVUbtdznkEVOxlkgjEa.#=qJDC4K5RuOV_AhLy8RlapFQ==(UInt32 #=qldhJZJ3FdEMBE_Ip4U0rYA==, Int32 #=qqcOEjKWuiA0UbQbqPPzjGw==, Boolean #=qmEhV5bck9mllqv8NjQ5bYg==)
bei #=q4KDzEOexdf9g9V0IF3u90ebtBbwQ8W$TQVFCcH8WL6OruEVUbtdznkEVOxlkgjEa.#=q7mWCN3dL0nhqoehpGiJmcA==()
bei #=q4KDzEOexdf9g9V0IF3u90ebtBbwQ8W$TQVFCcH8WL6OruEVUbtdznkEVOxlkgjEa.#=qZsRLWSlGdk8hO784nmM0tQ==(Stream #=qTqQaf7kpzZcHQSEyDnB0yg==)
bei #=q4KDzEOexdf9g9V0IF3u90ebtBbwQ8W$TQVFCcH8WL6OruEVUbtdznkEVOxlkgjEa…ctor(Stream #=qTqQaf7kpzZcHQSEyDnB0yg==)
bei Aspose.Email.Tools.FileFormatUtil.#=qfY5J8vDtLy_PS1vxkaSbCw==(Stream #=qTqQaf7kpzZcHQSEyDnB0yg==)


We are running the tests on the same environment as you have specified. Can you please share with us where are you checking the inner exceptions at your end inside visual studio? This will help us investigate the issue further at our end.

Well, the exception is thrown in the line where the message is loaded after several interations of the loop. When running the project in visual studio, the debugger stops in this line and you can get the details of the exception object.
When running the executable of the project directly, the exception is shown in a .NET-Framework message, where the exception is listed in the dialog.
exception.jpg (70.5 KB)


Could you please try it with the latest version of Aspose.Email for .NET 18.2? We are not able to reproduce the problem even on 32-bit operating system which usually give raise to such issues. Please try it with the latest version of the API at your end and let us know your feedback.

I’ve tried the following things:

  • updated to Aspose.Email.dll 18.2, using x86 as application platform: same exception
  • set application platform to any cpu: process is executed as x64-application, same behaviour (memory usage grows easily over 4+ gigs, i’ve killed the process before it finished).
  • tested the app on a Windows 2008 server R2 machine (VM): same behaviour and exception.

I don’t understand why this behaviour does not occur on your side. Don’t you see the growing memory usage in the task manager either?

What else can we do to find the cause? Can i activate some kind of logging for the mail loading in the Aspose.Email.dll? Or can you provide an assembly with added debug logging?

Thank you for your support!


The overall memory usage doesn’t exceed 45 MB at any particular time at our end during the execution of your application. There is no other logging facility that can be activity in this case for further investigation. I have attached a sample console application here that contains the same code as yours. Can you please run it and share if it also causes issue?OutOfMemory.zip (657.2 KB)

With your project, the behaviour does indeed not occur.
It seems, that the class attribute “[STAThread]” (single-thread apartment) is responsible for the issue.When commented out, the memory is disposed correctly When i add this attribute to your project, the memory issue also happens.

As we need this attribute in our application, can you please provide a fix for this?

Thank you and best regards!


At present, we are not able to even reproduce this issue with your sample application and are further investigating this at our end. We’ll get back to you on this soon with our findings.


The reported issue has been logged as EMAILNET-38941 for further investigation, though we are not able to reproduce it at our end. We’ll further investigate it for possible reproduction of the same and update you with our findings.

The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by kashif.iqbal