Memory Issues when converting text file to PDF

We’re running into memory problems when using Aspose.Words for .NET (version 21.5.0) to save big files to PDF format.

  • When converting bigger files like about 15mb, noticed that the memory went up to 1.5Gb and when we do multiple converts at a time it gets even worse.
  • There’s a memory leak when converting 2 large files at a time. The use case is, convert 1 file, memory goes up then convert another one at the same time, the memory ends up to be ~3gb. After the conversion completes, the memory went down to 1.5 and stayed there.

We have limited the file size to 50mb but it looks like we needed to lower this even more given the issues above.

Here’s how we implemented it:

public void Convert(Stream fileStream, Stream pdfFileStream, int maxPages)
        {
            var document = new Document(fileStream);
            var pdfSaveOptions = new PdfSaveOptions
            {
                MemoryOptimization = true,
                PageSet = new PageSet(
                    new PageRange(
                        0,
                        maxPages))
            };

            document.Save(pdfFileStream, pdfSaveOptions);
        }

Is there anything missing here?

Looking forward to hearing from you! Thanks

@jenquil

Please note that it is quite difficult to answer such questions because CPU performance and memory usage all depend on complexity and size of the documents you are loading/generating.

If you are loading huge Word documents into Aspose.Words’ DOM, more memory would be required. This is because during processing, the document needs to be held wholly in memory. Usually, Aspose.Words needs 10 times more memory than the original document size to build a DOM in the memory.

It hardly depends on local environment. It can be completely different for a server that generates thousands documents 24/7 or for a local PC that generate only the one document by demand.

We suggest you please use SaveOptions.MemoryOptimization property to optimize the memory performance. Setting this option to true can significantly decrease memory consumption while saving large documents at the cost of slower saving time.

Hi, thanks for your response.

We are already using SaveOptions.MemoryOptimization as written above. Tried setting it on and off, but still the memory consumption is 1gb+ for converting a 15mb file.

Upon debugging, it is indeed the load that’s taking so much memory. Just before save, the memory has already gone up, save was actually fast and only a little jump on memory.

@jenquil

Please share your input Word document with us for testing. We will investigate the issue and provide you more information on it.

Please find the test files here Test Files

It is the large text file that is causing the memory to shoot up to 1gb+

When the license is in trial/evaluation mode, loading the same large file and the conversion is pretty fast and the memory is only ~300mb, and probably because it just loaded and saved only the first 4 pages.

I wonder if the same behavior can be achieved with the Total License because we only need to load and save max of 100 pages.

@jenquil

Please spare us some time for the investigation of this issue. We will get back to you soon.

@jenquil

We have tested the scenario with both documents using the latest version of Aspose.Words for .NET 21.8 at Windows 10, 16 GB RAM and have not faced the shared issue. So, please use the latest version of Aspose.Words.

Hi, I tested with the latest version of Aspose.Words and I still get 1GB+ memory when converting the Large Text File. May I know how you implemented it?

@jenquil

We tested your scenario using following simple code example.

var document = new Document(MyDir + "input.txt");
document.Save(MyDir + "output.pdf", SaveFormat.Pdf);

Please note that Aspose.Words needs 10 times more memory than the original document size to build a DOM in the memory.

If it’s 10 times more then that would have been around 150mb for a 15mb file. Not really sure why it seems to work on your side but for me it’s 1GB+.

@jenquil

Please make sure that you are using the same document. Could you please share your working environment along with RAM status when you execute your application?