Uploaded image for project: 'PDFBox'
  1. PDFBox
  2. PDFBOX-3721

PDFMergerUtility is throwing java.lang.OutOfMemoryError: Java heap space exception while trying to merge documents

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical
    • Resolution: Incomplete
    • Affects Version/s: 2.0.5
    • Fix Version/s: None
    • Component/s: Utilities
    • Labels:
    • Flags:
      Important

      Description

      My code snapshot is like this:

      ByteArrayOutputStream mergedPdfStream = new ByteArrayOutputStream();
      PDFMergerUtility pdfMerger = new PDFMergerUtility();
      pdfMerger.setDestinationStream(mergedPdfStream);
      //looping through whole list of documents
      pdfMerger.addSource(new ByteArrayInputStream(bos.toByteArray()));
      //loop ends
      pdfMerger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());

      I am getting below exception
      java.lang.OutOfMemoryError: Java heap space
      at java.util.Arrays.copyOf(Arrays.java:3236)
      at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
      at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
      at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153)
      at org.apache.pdfbox.pdfwriter.COSStandardOutputStream.write(COSStandardOutputStream.java:129)
      at org.apache.pdfbox.io.IOUtils.copy(IOUtils.java:68)
      at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:1193)
      at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:383)
      at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:530)
      at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObjects(COSWriter.java:459)
      at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:443)
      at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1088)
      at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:419)
      at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1359)
      at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1246)
      at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1146)
      at org.apache.pdfbox.multipdf.PDFMergerUtility.mergeDocuments(PDFMergerUtility.java:280)

      We tried to increase memory also, which will allows only few more documents. Soon after that, I am getting same exception.

      is there any better solution to fix this.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              anilanapa anil kumar
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: