When attempting to optimize a 250MB file, an out-of-memory exception occurred

Sample code:

Aspose.Pdf.License license = new Aspose.Pdf.License();
license.SetLicense(“Aspose.Pdf.lic”);
Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(FullPath);
if (!PDFmodified && !pdfDoc.IsLinearized) // When PDF is NOT modified, do not save PDF using Aspose. Just Linearize using BitMiracle
{
string newfile = FullPath.Replace(“.pdf”, “$temp$.pdf”);
ARUtil.copyFilesToRepo(FullPath, newfile);
pdfDoc.IsLinearized = true;
pdfDoc.Save(newfile);
pdfDoc.Dispose();
pdfDoc = null;
}

However, when executing pdfDoc.Save(newfile); for a file that is 250 MB and has around 16k pages, a memory exception occurs. How can we fix this issue?

@prnksheela

When dealing with large PDF files, such as your 250MB document with around 16,000 pages, encountering an out-of-memory exception during the optimization process can be challenging. Here are some strategies to help mitigate this issue:

  1. Optimize Resources: Instead of directly saving the document, consider using the OptimizeResources method provided by the Aspose.PDF library. This method allows you to optimize the PDF document’s resources, which can significantly reduce memory usage.

  2. Set Optimization Options: Before optimizing, you can specify various optimization options using the OptimizationOptions class. This allows you to control aspects such as removing duplicate streams, unused objects, and compressing images. By fine-tuning these options, you can reduce the memory footprint during the optimization process.

  3. Process in Chunks: If the document is too large to handle in memory, consider processing it in smaller chunks. You can split the document into smaller parts, optimize each part individually, and then merge them back together if necessary.

  4. Increase Memory Allocation: If possible, increase the memory allocation for your application. This can be done by adjusting the settings in your development environment or server configuration.

  5. Use 64-bit Environment: Ensure that your application is running in a 64-bit environment, as this allows for greater memory allocation compared to a 32-bit environment.

Here is a sample code snippet demonstrating how to set optimization options and optimize the PDF document:

Aspose.Pdf.License license = new Aspose.Pdf.License();
license.SetLicense("Aspose.Pdf.lic");

Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(FullPath);

// Set optimization options
OptimizationOptions optimizationOptions = new OptimizationOptions
{
    RemoveUnusedObjects = true,
    RemoveUnusedStreams = true,
    CompressImages = true,
    ImageQuality = 75 // Adjust image quality as needed
};

// Optimize resources
pdfDoc.OptimizeResources(optimizationOptions);

// Save the optimized PDF document
string newfile = FullPath.Replace(".pdf", "$optimized$.pdf");
pdfDoc.Save(newfile);
pdfDoc.Dispose();

By following these steps, you should be able to reduce the memory usage during the optimization process and avoid out-of-memory exceptions.

If you continue to experience issues, consider reaching out to Aspose support for further assistance.

Sources:
[1]: Optimize File Size In PDF File