Memory leak while converting PST to PDF (C# .NET)

Hi,
We have memory leak in Aspose.Email current version 19.4.0.
We use Aspose.Email in our application server converting Email to Pdf.
If I load 1000 emails and convert them to pdf, then our server has about 1GB memory that never released. Now I downgrade Aspose.Email to version 6.5.0 then it works fine.
We need a solution urgently.

Thanks
Zhentao

@zhentaosong,

Can you please share source file along with sample code so that we may further investigate to help you out.

Hi, Muhammad,
the following code is called with each mail, after that all objects include MailMessage and MemoryStream are released with Dispose correctly.

Thnaks
Zhentao

private void ImportFromFile(string filename)
{
if (MapiMessage.IsMsgFormat(filename))
{
mOutlookMessage = MapiMessage.FromFile(filename);
mOutlookMessage.Save(MessageStream = new MemoryStream());
}
else
{
mAsposeMessage = MailMessage.Load(filename);
mAsposeMessage.PreferredTextEncoding = Encoding.UTF8;
mAsposeMessage.Save(MessageStream = new MemoryStream(), SaveOptions.DefaultMsg);
mOutlookMessage = MapiMessage.FromStream(MessageStream);
}
}

public override bool TryConvert(IContentStreamEntry streamEntry, FileInfo targetFile, onvertVersionDescriptordescriptor)
{
MailMessage mail = null;
try
{
if (MapiMessage.IsMsgFormat(streamEntry.GetContentStream()))
{
mail = MailMessage.Load(streamEntry.GetContentStream(), new MsgLoadOptions());
}
else
{
mail = MailMessage.Load(streamEntry.GetContentStream(), new EmlLoadOptions());
}
if (mail == null)
{
return false;
}
using (var stream = new MemoryStream())
{
mail.TimeZoneOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
var saveOptions = new PdfSaveOptions();
//Save as MHTML
mail.Save(stream, SaveOptions.DefaultMhtml);
//create an instance of LoadOptions and set the LoadFormat to Mhtml
var loadOptions = new LoadOptions();
loadOptions.LoadFormat = LoadFormat.Mhtml;
//create an instance of Document and load the MTHML from MemoryStream
var mailDocument = new Document(stream, loadOptions);
//save the document to Html file
mailDocument.Save(targetFile.FullName, saveOptions);
}
return true;
}
catch (Exception e)
{
Log.Debug("TryConvert return false: " + e.Message);
return false;
}
finally
{
if (mail != null)
mail.Dispose();
}
}

@zhentaosong,

Can you please share source emails for further investigation along with environment details.

Hi, Muhammad,
in zip file is my source mails. You have to run my code in service application.

Thanks
Zhentao

@zhentaosong,

I am unable to observe any zip file attached. Secondly, please provide a working sample solution reproducing the issue along with snapshot of issue incurring on your end. We may be able to help you further on provision of requested information.

Hello support team,
I sent you a sample program by mail. This way you can reproduce the problem. My question is that the same code works with Aspose.Email version 6.5.0, and it also works when I start the application in UI mode.

Thanks
Zhentao Song

@zhentaosong,

I have not been able to observe any email from your side. Can you please share the sample project here so that we may observe and test that on our end.

How can I share my zip-file. I try to upload. It doesn’t work.
Where can I share a file?

@zhentaosong,

You may share file via Dropbox and inform us here so that we may help you out.

Here is link

@zhentaosong,

I have worked with source file shared by you. An issue with ID EMAILNET-39468 has been created in our issue tracking system to further investigate and resolve the issue. We will share good news with you as soon as the issue will be fixed.

The problem occurs only when I call LoadAsposeLicense() in Service Main, I move LoadAspose License to OnStart, then the error does not occur.

@zhentaosong,

Its good know that issue has been resolved on your own. Can you please confirm if we can close issue now?

I want to know what the difference is when I load license in Main methode and OnStart mehtode. Are there any other effects when I call Load License in OnStart methode?
Why is this problem not present with older Aspose versions?

Best regards
Zhentao

@zhentaosong,

Thank you for providing addition information regarding issue arising on calling LoadAsposeLicense() in Service Main. I have updated the information in our issue tracking system and will get back to you as soon as the issue will be fixed and request for your patience in this regard.

Do you have any information for this issue?

Thanks
Zhentao

@zhentaosong,

I like to inform that we have investigated issue in details and like to share our findings with you. We have investigated that this is not a bug in Aspose.Email. The problem occurs when you did not use reference to Aspose.Email but converted mhtml files to pdf using Aspose.Word only(see attached solution). We was able to find out that reason of this issue is apply license for Aspose.Slides and(or) Aspose.Imaging product. As result you can see in memory many byte arrays after using System.Seflection.Emit.DynamicMethod(see screenshot). ServiceAsposeTest2.zip (715.4 KB)
ants.png (70.1 KB)

Hi,

I don’t understand what the solution is for me.
What should I change in my code?

Thanks
Zhentao

@zhentaosong

Please note that performance and memory usage all depend on complexity and size of the PST for PDF conversion.

We suggest you please try the latest version of Aspose.Email for .NET. Hope this helps you.