Aspose.PDF.Document.Save(Stream output) is throwing SystemOutOfMemory exception

We are using one WPF C# project of .Net Framework version 4.6.1 in Visual studio 2017 to create one PDF using Aspose.PDF.dll of version 23.5.0.0
But whenever the Save(Stream output) method of Aspose.PDF.Document class is called for some documents with multiple numbers of tables System.OutOfMemory exception is thrown .
We have used Aspose.PDF.Document.FreeMemory() and Aspose.PDF.Document.Optimize() both before Aspose.PDF.Document.Save(Stream output) , but none of them resolve the issue.

@prmukherjee

The System.OutOfMemory exception when calling Aspose.PDF.Document.Save(Stream output) can occur due to the size or complexity of the PDF document, particularly if it contains multiple tables. Here are some steps to troubleshoot and potentially resolve the issue:

  1. Increase Memory Allocation: Ensure that your application has sufficient memory allocated. Check the system’s available memory and consider running the application on a machine with more RAM.

  2. Optimize Document: Make sure you are correctly using Aspose.PDF.Document.FreeMemory() and Aspose.PDF.Document.Optimize(). Call these methods at appropriate times in your code to free up resources.

  3. Split the Document: If feasible, split the document into smaller parts and save them separately. This can help reduce memory usage during the save operation.

  4. Use a Different Stream: Experiment with different types of streams, such as MemoryStream or FileStream, to see if they affect memory usage.

  5. Check for Large Images or Resources: If your PDF contains large images or resources, consider compressing them or reducing their size before adding them to the document.

  6. Update Aspose.PDF: Ensure you are using the latest version of Aspose.PDF, as updates may include performance improvements and bug fixes.

If the issue persists, consider reaching out to Aspose support with a minimal reproducible example of your code and the specific document causing the issue.

Hello,

We are creating some sections in PDF documents which contains one or multiple tables,
we are using table format like below:

var tbl39 = PDFHelper.CreateTable(7, isLargeTable: true);
// document.Add(tbl39);

try
{
    // Basic headers
    tbl39.AddHeaderCell(PDFHelper.CreateTableHeaderCell("EPP NO"));
    tbl39.AddHeaderCell(PDFHelper.CreateTableHeaderCell("EPP ID"));
    tbl39.AddHeaderCell(PDFHelper.CreateTableHeaderCell("Node 1"));
    tbl39.AddHeaderCell(PDFHelper.CreateTableHeaderCell("Node 2"));
    tbl39.AddHeaderCell(PDFHelper.CreateTableHeaderCell("Y1 \n (m)"));
    tbl39.AddHeaderCell(PDFHelper.CreateTableHeaderCell("Y2 \n (m)"));

    // Net thickness
    tbl39.AddHeaderCell(
        PDFHelper.CreateTableHeaderCell(
            PDFHelper.CreateParagraph("t")
                .AddSub("net")
                .Add("\n (mm)")
        )
    );

    // Qv
    tbl39.AddHeaderCell(PDFHelper.CreateTableHeaderCell("Qv \n (1 / mm)"));

    // Stress: σHG1_Hog
    tbl39.AddHeaderCell(
        PDFHelper.CreateTableHeaderCell(
            PDFHelper.CreateParagraph("")
                .AddSymbol(Symbols.Sigma)
                .AddSub("HG1_Hog")
                .Add("\n (kgf / mm²)")
        )
    );

    // Stress: σHG1_Sag
    tbl39.AddHeaderCell(
        PDFHelper.CreateTableHeaderCell(
            PDFHelper.CreateParagraph("")
                .AddSymbol(Symbols.Sigma)
                .AddSub("HG1_Sag")
                .Add("\n (kgf / mm²)")
        )
    );

    // Stress: σHG2_Hog
    tbl39.AddHeaderCell(
        PDFHelper.CreateTableHeaderCell(
            PDFHelper.CreateParagraph("")
                .AddSymbol(Symbols.Sigma)
                .AddSub("HG2_Hog")
                .Add("\n (kgf / mm²)")
        )
    );

    // Stress: σHG2_Sag
    tbl39.AddHeaderCell(
        PDFHelper.CreateTableHeaderCell(
            PDFHelper.CreateParagraph("")
                .AddSymbol(Symbols.Sigma)
                .AddSub("HG2_Sag")
                .Add("\n (kgf / mm²)")
        )
    );

    // Shear: τHG_Hog
    tbl39.AddHeaderCell(
        PDFHelper.CreateTableHeaderCell(
            PDFHelper.CreateParagraph("")
                .AddSymbol(Symbols.Tau)
                .AddSub("HG_Hog")
                .Add("\n (kgf / mm²)")
        )
    );

    // Shear: τHG_Sag
    tbl39.AddHeaderCell(
        PDFHelper.CreateTableHeaderCell(
            PDFHelper.CreateParagraph("")
                .AddSymbol(Symbols.Tau)
                .AddSub("HG_Sag")
                .Add("\n (kgf / mm²)")
        )
    );
}

which has 1 single row value.

for some section based on data this table is being created for around 350 to 500 times for 350 to 500 instances and for that type of section either at time of adding table to document sometimes its throwing System.OutOfMemory exception or at time of Document.Save(filestream) its throwing the same System.OutOfMemory exception.

Please help me if I am missing any necessary part to perform this report creation.

@prmukherjee

If possible, could you please share a sample console application in .zip format so that we can reproduce this issue in our environment and address it accordingly?