Java Out of Memory Error: Conversion of 51 MB pptx/word/excel file to pdf file

We are using Aspose TotalJava license to convert input files like (word, ppt, excel, cad, image etc ) to convert to pdf file.

But any file larger than 30 MB results in out of memory error

We are using max heap size of 2048 MB.

source file contents for ppt to pdf conversion:–>

protected File generatePdf(File pdfFile) {
    Presentation presentation = null;
    try {
        presentation = new Presentation(file.getAbsolutePath());
        PdfOptions pdfOptions = new PdfOptions();
        pdfOptions.setCompliance(PdfCompliance.PdfUa);
        presentation.save(pdfFile.getAbsolutePath(), SaveFormat.Pdf, pdfOptions);
        return pdfFile;
    } catch (Exception e) {
        log.error("Exception on generating pdf file from pptx/ppt", e);
    } finally {
        if (presentation != null) {
            presentation.dispose();
        }
    }
    return null;
}

Error logs below: →

java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferByte.(DataBufferByte.java:92) ~[?:?]
at java.awt.image.ComponentSampleModel.createDataBuffer(ComponentSampleModel.java:436) ~[?:?]
at sun.awt.image.ByteInterleavedRaster.(ByteInterleavedRaster.java:89) ~[?:?]
at sun.awt.image.ByteInterleavedRaster.createCompatibleWritableRaster(ByteInterleavedRaster.java:1276) ~[?:?]
at sun.awt.image.ByteInterleavedRaster.createCompatibleWritableRaster(ByteInterleavedRaster.java:1287) ~[?:?]
at com.aspose.slides.internal.g2.int.new(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.internal.g2.int.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.internal.fs.if.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.internal.fs.if.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.internal.fs.if.if(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.internal.fs.if.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.internal.fs.if.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.internal.fs.if.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.internal.fs.if.deepClone(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.dj.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.dj.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.dj.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.e.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.s8.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.ain.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.ain.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.ud.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.arh.for(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.arh.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.arh.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.Slide.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.alf.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.alf.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.Presentation.do(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at com.aspose.slides.Presentation.save(Unknown Source) ~[aspose-slides-22.6-jdk16.jar:22.6]
at nl.assai.ocr.service.pdfgenerator.SlidesToPdfGenerator.generatePdf(SlidesToPdfGenerator.java:30) ~[classes/:?]

can you please check it and provide solution to avoid this memory error?

@officeteam

To address the Out of Memory error you are encountering when converting large PPTX, Word, or Excel files to PDF using Aspose, there are several strategies you can implement to optimize memory usage:

  1. Increase Java Heap Size: Since you are currently using a maximum heap size of 2048 MB, consider increasing it further if your system allows. This can be done by adjusting the JVM options when starting your application. For example:

    java -Xmx4096m -jar your-application.jar
    
  2. Use Temporary Folders: Utilize temporary folders for loading and saving files. This can help reduce memory consumption during processing. You can set the temporary folder for loading and saving options as follows:

    LoadOptions loadOptions = new LoadOptions();
    loadOptions.setTempFolder("path/to/temp/folder");
    
    PdfSaveOptions saveOptions = new PdfSaveOptions();
    saveOptions.setTempFolder("path/to/temp/folder");
    
  3. Enable Memory Optimization: Aspose provides a memory optimization feature that can be enabled to reduce the memory footprint during document processing. You can enable this option in the save options:

    PdfSaveOptions saveOptions = new PdfSaveOptions();
    saveOptions.setMemoryOptimization(true);
    
  4. Downsample Images: If your documents contain high-resolution images, consider downsampling them to reduce memory usage. You can set the resolution for images in the PDF save options:

    PdfSaveOptions saveOptions = new PdfSaveOptions();
    saveOptions.getDownsampleOptions().setResolution(150); // Set to desired resolution
    
  5. Process in Smaller Chunks: If possible, break down the document into smaller parts and process them individually. This can help manage memory usage more effectively.

  6. Check for Memory Leaks: Ensure that you are properly disposing of objects after use. For instance, make sure to call the dispose() method on the Presentation object after saving the PDF to free up resources.

By implementing these strategies, you should be able to mitigate the Out of Memory errors during the conversion of large files. If the problem persists, consider reaching out to Aspose support with specific details about your implementation and the files you are working with for further assistance.

@officeteam Could you please attach your document here for testing? We will check the issue and provide you more information. In Aspose.Words for reducing memory usage upon processing extremally large documents, you can try using LoadOptions.TempFolder , SaveOptions.TempFolder and SaveOptions.MemoryOptimization properties.

Please note, Aspose.Words always allocates more memory that actual document size. This is expected. Please see our documentation for more information:
https://docs.aspose.com/words/net/memory-requirements/

In addition, I will move this topic to the Aspose.Total category so that my colleagues from other teams can assist you with any other Aspose products you are using.

@officeteam,
We are sorry that you encountered this problem. We need more details to investigate the cases you described. Please share the following files and information:

  • sample files
  • OS version on which the errors occur
  • JDK target version in your application project

@officeteam,

To address the out of memory issue/error with Excel spreadsheets when converting them to PDF using Aspose.Cells for Java, could you please provide a sample Excel file? We will assess your issue soon.

P.S. We also suggest trying out the latest version of the API if you aren’t using it already.