Exception "Out Of Memory Error" on rendering presentation (Java)

What are the recommend JVM settings for heap size when using Aspose.Slides?

We’re having trouble rendering a relatively small presentation <70MB with a heap size of 4 gigabytes which seems excessive for such a small presentation.

The code we use for rendering looks like this:

String filename = args[0];
String outputFilename = args[1];


Presentation doc = new Presentation(filename);
System.out.println("Loaded document, getting ready to render");

PdfOptions options = new PdfOptions();
options.setSaveMetafilesAsPng(false);
options.setSufficientResolution(128f);

INotesCommentsLayoutingOptions notesOptions = options.getNotesCommentsLayouting();
notesOptions.setNotesPosition(NotesPositions.None);

Document combinedPdf = new Document(); // aspose.pdf.Document

int batchSize = 10;
int totalSlides = doc.getSlides().size();
int batches = (int) Math.ceil( ((double) totalSlides) / batchSize);
for(int batch = 1; batch <= batches; batch++) {
    int slideStart = ((batch-1) * batchSize) + 1;
    int slideEnd = (batch == batches) ? totalSlides : batch * batchSize ;
    System.out.println(String.format("Rendering batch %d", batch));

    int[] slides = IntStream.rangeClosed(slideStart, slideEnd).toArray();

    ByteArrayOutputStream batchOutputStream = new ByteArrayOutputStream();
    doc.save(batchOutputStream, slides, SaveFormat.Pdf, options);
    InputStream batchInputStream = new ByteArrayInputStream(batchOutputStream.toByteArray());
    Document batchDocument = new Document(batchInputStream);

    combinedPdf.getPages().add(batchDocument.getPages());
}
System.out.println("Batches done... saving");
combinedPdf.save(outputFilename);
System.out.println("Saving done...");

As you can see our code already renders the presentation in batches to reduce memory consumption.

Using a 4 gigabyte heap size with -Xmx4g we see the following error when rendering:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at com.aspose.slides.internal.e5.void.setCapacity(Unknown Source)
        at com.aspose.slides.internal.e5.void.b(Unknown Source)
        at com.aspose.slides.internal.e5.void.write(Unknown Source)
        at com.aspose.slides.internal.eu.boolean.new(Unknown Source)
        at com.aspose.slides.internal.eu.boolean.close(Unknown Source)
        at com.aspose.slides.internal.eu.finally.do(Unknown Source)
        at com.aspose.slides.internal.eu.finally.dispose(Unknown Source)
        at com.aspose.slides.internal.dv.break.do(Unknown Source)
        at com.aspose.slides.internal.ej.for.write(Unknown Source)
        at com.aspose.slides.internal.ef.import.do(Unknown Source)
        at com.aspose.slides.internal.ef.import.do(Unknown Source)
        at com.aspose.slides.internal.en.if.int(Unknown Source)
        at com.aspose.slides.internal.en.if.for(Unknown Source)
        at com.aspose.slides.internal.en.if.new(Unknown Source)
        at com.aspose.slides.internal.en.if.do(Unknown Source)
        at com.aspose.slides.internal.ef.double.do(Unknown Source)
        at com.aspose.slides.internal.ef.double.if(Unknown Source)
        at com.aspose.slides.internal.ef.short.do(Unknown Source)
        at com.aspose.slides.internal.ef.try.do(Unknown Source)
        at com.aspose.slides.internal.ef.else.do(Unknown Source)
        at com.aspose.slides.internal.ef.throw.do(Unknown Source)
        at com.aspose.slides.acs.do(Unknown Source)
        at com.aspose.slides.Presentation.do(Unknown Source)
        at com.aspose.slides.Presentation.save(Unknown Source)
        at com.asposeslidestest.Main.main(Main.java:61)

What are the recommended memory settings for Aspose.Slides? Is there a general rule that we need to allocate memory X times the size of a presentation or how do we calculate this?

Example Presentation: https://send.firefox.com/download/2b8ab67ee55f5ccc/#HTNCmVYOHzNfDJViLSthqA (note link expires after download or 1 day).

@sbd,

I have observed the information shared by you and like to share that heap size depends on size of presentation and number of slides, shapes inside presentation especially multimedia content. There is no recommended memory setting as it depends on afore mentioned factors.

Thanks for the feedback. Please share the Aspose guidelines for setting the correct heap size based on presentations size, slides number and multimedia.

@sbd,

I like to inform that we don’t have such guidelines, but by default we use following settings -Xms2048M -Xmx4096M. Please share feedback with us if there is still an issue.

Just FYI we have seen a big improvement in memory consumption after upgrading to Aspose.Slide 19.4.

@sbd,

Thank you for sharing valuable feedback with us.