Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: combine 2000-5000 PDFs into 1 using iText yield OutOfMemorryError
    primarykey
    data
    text
    <p>I have eyeballing this code for a long time, trying to reducing the amount of memory the code use and still it generated <code>java.lang.OutOfMemoryError: Java heap space</code>. As my last resort, I want to ask the community on how can I improve this code to avoid <code>OutOfMemoryError</code></p> <p>I have a driver/manifest file (.txt file) that contain information about the PDFs. I have about 2000-5000 pdf inside a zip file that I need to combine together. Before the combining, for each pdf, I need to add 2-3 more pdf pages to it. <code>Manifest</code> object holds information about a pdf.</p> <pre><code>try{ blankPdf = new PdfReader(new FileInputStream(config.getBlankPdf())); mdxBacker = new PdfReader(new FileInputStream(config.getMdxBacker())); theaBacker = new PdfReader(new FileInputStream(config.getTheaBacker())); mdxAffidavit = new PdfReader(new FileInputStream(config.getMdxAffidavit())); theaAffidavit = new PdfReader(new FileInputStream(config.getTheaAffidavit())); ImmutableList&lt;Manifest&gt; manifestList = //Read manifest file and obtain List&lt;Manifest&gt; File zipFile = new File(config.getInputDir() + File.separator + zipName); //Extracting PDF into `process` folder ZipUtil.extractAll(config.getExtractPdfDir(), zipFile); outputPdfName = zipName.replace(".zip", ".pdf"); outputZipStream = new FileOutputStream(config.getOutputDir() + File.separator + outputPdfName); document = new Document(PageSize.LETTER, 0, 0, 0, 0); writer = new PdfCopy(document , outputZipStream); document.open(); //Open the document //Start combining PDF files together for(Manifest m : manifestList){ //Obtain full path to the current pdf String pdfFilePath = config.getExtractPdfDir() + File.separator + m.getPdfName(); //Before combining PDF, add backer and affidavit to individual PDF PdfReader pdfReader = PdfUtil.addBackerAndAffidavit(config, pdfType, m, pdfFilePath, blankPdf, mdxBacker, theaBacker, mdxAffidavit, theaAffidavit); for(int pageNumber=1; pageNumber&lt;=pdfReader.getNumberOfPages(); pageNumber++){ document.newPage(); PdfImportedPage page = writer.getImportedPage(pdfReader, pageNumber); writer.addPage(page); } } } catch (DocumentException e) { } catch (IOException e) { } finally{ if(document != null) document.close(); try{ if(outputZipStream != null) outputZipStream.close(); if(writer != null) writer.close(); }catch(IOException e){ } } </code></pre> <p>Please, rest assure that I have look at this code for a long time, and try rewrite it many times to reduce the amount of memory it using. After the OutOfMemoryError, there are still lots of pdf files that have not been added 2-3 extra pages, so I think it is inside <code>addBackerAndAffidavit</code>, however, I try to close every resources I opened, but it still exception out. Please help. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload