We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

OutOfMemory Exception occurs while Converting PDF to PDF/A2b using Java

Hi, we are evaluating your Aspose.PDF java library. We need to convert some pdf documents into PDF/A-2b. These documents are large (between 400 and 600 pages). Trying this code:

File filePathContract = new File(filePath);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(FileUtils.readFileToByteArray(filePathContract));
byte[] fileContentByteArray = IOUtils.toByteArray(byteArrayInputStream);
ByteArrayOutputStream pdfAOS = new ByteArrayOutputStream();
if (Document.isLicensed()) {
ByteArrayInputStream pdfAIS = new ByteArrayInputStream(fileContentByteArray);
com.aspose.pdf.Document pdfADocument = new com.aspose.pdf.Document(pdfAIS);
pdfADocument.convert("./1234_conversionLog.xml", PdfFormat.PDF_A_2B, ConvertErrorAction.Delete);
pdfADocument.save(pdfAOS);
pdfADocument.close();
}

with 1.5 GB of maximum heap size (we would not want to increase this value) we get an OutOfMemoryException:

Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
at com.aspose.pdf.internal.l20k.l29t.(Unknown Source)
at com.aspose.pdf.internal.l20k.l29t.lI(Unknown Source)
at com.aspose.pdf.internal.l6p.ld.l0j(Unknown Source)
at com.aspose.pdf.internal.l6p.lh.lI(Unknown Source)
at com.aspose.pdf.internal.l6p.lh.(Unknown Source)
at com.aspose.pdf.internal.l6p.l0t.lI(Unknown Source)
at com.aspose.pdf.internal.l6p.l0t.lI(Unknown Source)
at com.aspose.pdf.internal.l10t.ly.lI(Unknown Source)
at com.aspose.pdf.internal.l10t.ly.lI(Unknown Source)
at com.aspose.pdf.internal.l10t.ly.l0k(Unknown Source)
at com.aspose.pdf.internal.l10t.ly.lf(Unknown Source)
at com.aspose.pdf.internal.l10t.l0if.l0t(Unknown Source)
at com.aspose.pdf.internal.l10t.ly.lI(Unknown Source)
at com.aspose.pdf.ADocument.lI(Unknown Source)
at com.aspose.pdf.ADocument.convert(Unknown Source)
at com.aspose.pdf.Document.convert(Unknown Source)
at com.aspose.pdf.ADocument.convert(Unknown Source)
at com.aspose.pdf.Document.convert(Unknown Source)
at com.prova.pdfaconverter.Main.main(Main.java:36)

The document (about 18MB) used in our test is: https://1drv.ms/b/s!AtPn2aivHcLuhD3zGo5Ibg5GRcdM?e=hzlDkx

You can help us? Is our code correct? Can we optimize the heap utilization? We are trying other commercial libraries and Apose PDF seems the most intuitive, but we would not like to receive OutOfMemories at run-time.

Thanks!

@marco.caldirola

We tested the scenario in our environment and noticed that the program took more than 15 minutes but never generated any output. We had to terminate the process. Therefore, an issue as PDFJAVA-41172 has been generated in our issue tracking system to investigate it further. We will look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.

@marco.caldirola

We investigated the earlier logged ticket and found that 500Mb of the heap (-Xmx500M) is enough for conversion when use next code snipped:

            com.aspose.pdf.Document pdfADocument = new com.aspose.pdf.Document(pdfAIS);
            PdfFormatConversionOptions convert = new PdfFormatConversionOptions(
                    "./1234_conversionLog.xml", PdfFormat.PDF_A_2B, ConvertErrorAction.Delete);
            convert.setPageByPageFontProcess(true);
            pdfADocument.convert(convert);
            pdfADocument.setSkipPdfaCompliantValidationBeforeSave(true);
            pdfADocument.save(pdfAOS);
            pdfADocument.close();