Aspose.Slides Is Using a Lot of Memory for Saving PPTX as PDF in C#

Aspose Slides using a lot of memory for save pptx as pdf
I am trying to generate a Pdf using Aspose slides for .NET. I am using version 24.11
My presentation 50 mb. When calling the Save method, memory usage 4-5 gb and doesn’t get cleared up after.

My example

using Aspose.Slides;
using System.Diagnostics;

internal class Program
{
    private static void Main(string[] args)
    {
        Console.WriteLine("start");
        logMemory();
        Console.WriteLine(OpenSaveAs("C:\\Temp\\temp2.pptx", "C:\\Temp\\temp2.pdf"));
        logMemory();
        Console.WriteLine("end");
        
    }
    static void logMemory()
    {
        var workingSet = 0.0;
        Process proc = Process.GetCurrentProcess();
        workingSet = proc.WorkingSet64 / (1024 * 1024);
        proc.Dispose();
        Console.WriteLine("Working Set in MBs: " + workingSet);
    }
    static int OpenSaveAs(string sFilePath, string newPath)
    {
        

        using (var stream = File.OpenRead(sFilePath))
        {
            var loadOptions = new Aspose.Slides.LoadOptions();
            loadOptions.LoadFormat = LoadFormat.Ppt;

            loadOptions.BlobManagementOptions = new BlobManagementOptions();
            loadOptions.BlobManagementOptions.MaxBlobsBytesInMemory = 4 * 1024 * 1024;
            loadOptions.BlobManagementOptions.IsTemporaryFilesAllowed = true;
            loadOptions.BlobManagementOptions.PresentationLockingBehavior = PresentationLockingBehavior.KeepLocked;
            var tempPresentation = new Aspose.Slides.Presentation(stream, loadOptions);
            tempPresentation.Save(newPath, Aspose.Slides.Export.SaveFormat.Pdf);
            tempPresentation.Dispose();
        }

        return 0;
    }
}

@PLitvin

When converting a large PPTX file to PDF using Aspose.Slides, high memory usage can be a common issue. Here are some strategies to help manage memory consumption during this process:

  1. Garbage Collection: After saving the presentation, explicitly call the garbage collector to free up memory. This can be done using GC.Collect() in your code. It is important to note that memory statistics may not reflect the actual usage until garbage collection is performed.

  2. Disposing Objects: Ensure that you are properly disposing of the Presentation object after use. This can be done by calling the Dispose() method, which helps in releasing the resources held by the object.

  3. Blob Management Options: You can configure blob management options to control how memory is used for large objects. For example, you can set the maximum size of blobs that can be kept in memory and allow temporary files to be used. Here’s how you can modify your existing code:

    var loadOptions = new Aspose.Slides.LoadOptions();
    loadOptions.BlobManagementOptions = new BlobManagementOptions
    {
        MaxBlobsBytesInMemory = 4 * 1024 * 1024, // Set max size for blobs in memory
        IsTemporaryFilesAllowed = true, // Allow temporary files
        PresentationLockingBehavior = PresentationLockingBehavior.KeepLocked
    };
    
  4. Testing with Different Settings: If you continue to experience high memory usage, consider adjusting the MaxBlobsBytesInMemory to a lower value or setting it to zero to force all blobs to be written to temporary files. This can significantly reduce memory consumption.

  5. Monitoring Memory Usage: Use the Process class to log memory usage before and after the conversion process. This will help you understand how much memory is being consumed and whether your optimizations are effective.

Here’s an updated version of your OpenSaveAs method that includes garbage collection and proper disposal:

static int OpenSaveAs(string sFilePath, string newPath)
{
    using (var stream = File.OpenRead(sFilePath))
    {
        var loadOptions = new Aspose.Slides.LoadOptions
        {
            BlobManagementOptions = new BlobManagementOptions
            {
                MaxBlobsBytesInMemory = 4 * 1024 * 1024,
                IsTemporaryFilesAllowed = true,
                PresentationLockingBehavior = PresentationLockingBehavior.KeepLocked
            }
        };

        using (var tempPresentation = new Aspose.Slides.Presentation(stream, loadOptions))
        {
            tempPresentation.Save(newPath, Aspose.Slides.Export.SaveFormat.Pdf);
        }
    }

    GC.Collect(); // Explicitly call garbage collection
    return 0;
}

By implementing these strategies, you should be able to reduce the memory footprint when converting large presentations to PDF. If the problem persists, consider reaching out to Aspose support for further assistance.

Sources:
[1]: PPTX to PDF - Memory usage problem

@PLitvin,
If the issue persists, please share the following:

  • sample presentation file (you can share a link to the file saved in a file storage)
  • OS version on which the problem occurs
  • .NET target version in your app project