0

I am trying to merge around 15 pdfs into a single PDF. It's working most of the time but sometimes getting OutofMemory Java Heap Space error. I want to avoid temp file creation. Below is my code .

public static byte[] mergePdf(List < InputStream > inputStreams) {
    Document document = new Document();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    PdfCopy copy = new PdfSmartCopy(document, byteArrayOutputStream);
    document.open();
    for (InputStream inputStream: inputStreams) {
        PdfReader pdfReader = new PdfReader(inputStream);
        copy.addDocument(pdfReader);
        copy.freeReader(pdfReader);
        pdfReader.close();
    }
    document.close();
    return byteArrayOutputStream.toByteArray();
}
Mikheil Zhghenti
  • 734
  • 8
  • 28
VKP
  • 589
  • 1
  • 8
  • 34
  • I think this post should solve your problem. If it does, please let me/us know And we'll close your question as a duplicate, okay? https://stackoverflow.com/questions/1565388/increase-heap-size-in-java – ControlAltDel Oct 28 '22 at 19:30
  • Unfortunately No , I cant change the heap size . This failing in AWS after deploying the application . Is there any better way to code this method – VKP Oct 28 '22 at 19:34
  • I believe you can change your heap size on AWS. I found this: https://stackoverflow.com/questions/63773664/how-to-increase-java-heap-size-in-aws-ecs as well as another post on SO about how to solve this. – ControlAltDel Oct 28 '22 at 19:49
  • I think you can improve it by marking each of the inputStream from your list, for garbage collection(inputStream=null) once you already add it to your copy of the pdf. – RohithPg Oct 28 '22 at 19:57

1 Answers1

1

If you send it somwhere to OutputStream (eg. thru http - which seems to be true as you don't want to store it on disk) then instead of using intermediate ByteArrayOutpuStream (which is the RAM hog) - send it to the OutputStream directly - pass the OutputStream to the method creating the pdf, in this way you'll avoid holding it in memory at all.

Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
  • i am trying to save the file in to DB . The data type in Oracle is BLOB and the entity type is byte[]. – VKP Oct 28 '22 at 20:36
  • Then you can use BlobOutputStream https://docs.oracle.com/database/121/IMJVC/oracle/ord/media/jai/io/BlobOutputStream.html – Krzysztof Cichocki Oct 29 '22 at 09:40