Aspose.PDF memory issues

Hi,
I’m operating on big pdf file (800MB), attached.
I used Aspose.PDF for .NET, version 25.11 and 26.1.

The desired result is to ensure in that file some metadata tags, I prepared 2 versions of code to achieve that.
I cannot use any middle-layer files, so temporary dump to disc is not possible.
The test scripts operates on FileStream, but in my production scenario file is hosted.

1st script works directly on the opened FileStream:

void Main()
{
	using (var outputStream = new MemoryStream())
    {
        // Some source stream, that needs to be loaded to memory afterward
        var tempStream = File.OpenRead($@"D:\temp\Test_22_A22222B.pdf");

        using (var pdfDocument = new Aspose.Pdf.Document(tempStream))
        {
            pdfDocument.Info["test"] = "True";
            pdfDocument.Save(outputStream);
        }
    }
}

The exeuction in that scenario is taking a huge amount of time > 40 minutes.

2nd approach:

void Main()
{
	using (var outputStream = new MemoryStream())
    {
        // Some source stream, that needs to be loaded to memory afterward
        var tempStream = File.OpenRead($@"D:\temp\Test_22_A22222B.pdf");
            using (var processedStream = new MemoryStream())
            {
                    tempStream.CopyTo(processedStream);
                    processedStream.Position = 0;
                    using (var pdfDocument = new Aspose.Pdf.Document(processedStream))
                    {
                        pdfDocument.Info["test"] = "True";
                        pdfDocument.Save(outputStream);
                    }
            }
    }
}

This operation took ~3,5 minutes, but consumed practiaclly whole available memory during the .Save call.

I’d like to ask:

  1. If there is any alternate approach to ensure the metadata tag in such big pdf file?
  2. Is there any alternative approach to .Save call() to reduce the memory utilization in 2nd approach? I cannot modify the content or other properties of pdf file, so OptimizeResources() cannot be used.
  3. May there be any memory leak issue in .Save() that results in observed behavior?
  4. I’m having issues with upload of test file, is there some alternative way to provide it to you, or it won’t be needed?

Best regards,
Artur

@ARTJ

You are trying to add a custom metadata entry to an ≈ 800 MB PDF with Aspose.PDF for .NET 25.11/26.1, and the operation takes more than 40 minutes while consuming a large amount of memory.

1. How to add the metadata
Aspose.PDF does not provide an in‑place “metadata‑only” update. The usual way is to open the PDF as a Document, modify the Info dictionary, and then save the document to a new stream.

2. Reducing memory usage when saving

  • Open the source PDF directly from its stream and avoid copying it into an intermediate MemoryStream. For example:
using (var src = File.OpenRead(@"D:\temp\Test_22_A22222B.pdf"))
using (var dst = new MemoryStream())   // or any other output stream
{
    var doc = new Aspose.Pdf.Document(src);
    doc.Info["test"] = "True";
    doc.Save(dst);                     // no extra copy of the source
}
  • Ensure that every Document and stream is wrapped in a using block so that native resources are released promptly.

3. Possible memory‑leak cause
There are reported memory‑leak issues when Document.Save writes to a MemoryStream in versions up to 24.5.1 [2]. Fixes for these leaks were introduced in later releases, and updating the library is recommended [8].

4. Next steps

  1. Upgrade to the latest Aspose.PDF for .NET version.
  2. Run the code without the extra MemoryStream copy and measure the memory/time consumption.
  3. If the problem persists, try to reproduce it with a smaller PDF sample and share that sample in the thread for further investigation.