title: PDFBOX java.lang.OutOfMemoryError: java heap space; GC overhead limit exceeded
body: <p>I am working on a Java project.
  I retrieve pdf from a server and convert it into images to compare pixels with the expected files. 
  The program is fine when I only run it with 20-30 files, but it throws me OutOfMemoryError when I try to use more than 30 files. </p>
  
  <p>It seems like the pdf -> image conversion method is producing the OutOfMemoryError. </p>
  
  <p>Here is the method:</p>
  
  <pre><code>    PDDocument document = null;
      try {
          document = PDDocument.loadNonSeq(new File(pdfFile), null);
          List&lt;PDPage&gt; pages = new ArrayList&lt;PDPage&gt;();
          for (Object obj : document.getDocumentCatalog().getAllPages()) {
              PDPage page = (PDPage) (obj);
              pages.add(page);
          }
          int pageNum = 0;
          for (PDPage pdPage : pages) {
              BufferedImage img = pdPage.convertToImage(BufferedImage.TYPE_INT_RGB, 100);
              if (isExpected) {
                  ImageIOUtil.writeImage(img, FolderUtils.getImageFolder(websiteName) + File.separator + "expected"
                          + (++pageNum) + ".png", 100);
              } else {
                  ImageIOUtil.writeImage(img, FolderUtils.getImageFolder(websiteName) + File.separator + "rendered"
                          + (++pageNum) + ".png", 100);
              }
              img.flush();
              img = null;
          }
      } catch (IOException e) {
          debugLog.log(Level.SEVERE, e.getMessage(), e);
      } finally {
          if (document != null) {
              try {
                  document.close();
                  document = null;
              } catch (IOException e) {
                  debugLog.log(Level.SEVERE, e.getMessage(), e);
              }
          }
      }
  </code></pre>
  
  <p>And here is the part of the log for Java Heap Space:</p>
  
  <pre><code>Exception in thread "pool-1-thread-1" java.lang.OutOfMemoryError: Java heap space
      at java.awt.image.DataBufferInt.&lt;init&gt;(DataBufferInt.java:75)
      at java.awt.image.Raster.createPackedRaster(Raster.java:467)
      at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032)
      at java.awt.image.BufferedImage.&lt;init&gt;(BufferedImage.java:340)
      at org.apache.pdfbox.pdmodel.graphics.xobject.CompositeImage.createMaskedImage(CompositeImage.java:85)
      at org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage.applyMasks(PDXObjectImage.java:158)
      at org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap.getRGBImage(PDPixelMap.java:367)
      at org.apache.pdfbox.util.operator.pagedrawer.Invoke.process(Invoke.java:87)
      at org.apache.pdfbox.util.PDFStreamEngine.processOperator(PDFStreamEngine.java:557)
      at org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:268)
      at org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:235)
      at org.apache.pdfbox.util.PDFStreamEngine.processStream(PDFStreamEngine.java:215)
      at org.apache.pdfbox.pdfviewer.PageDrawer.drawPage(PageDrawer.java:139)
      at org.apache.pdfbox.pdmodel.PDPage.convertToImage(PDPage.java:801)
      at ********************.convertPDFToImages(Processor.java:107)
      at ********************.APIProcessor.run(APIProcessor.java:62)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>Here is the part of log for GC overhead limit exceed: </p>
  
  <pre><code>Exception in thread "pool-1-thread-3" java.lang.OutOfMemoryError: GC overhead limit exceeded
      at java.util.Collections.singletonList(Collections.java:3349)
      at org.apache.pdfbox.pdfparser.NonSequentialPDFParser.parseDictObjects(NonSequentialPDFParser.java:1275)
      at org.apache.pdfbox.pdfparser.NonSequentialPDFParser.initialParse(NonSequentialPDFParser.java:414)
      at org.apache.pdfbox.pdfparser.NonSequentialPDFParser.parse(NonSequentialPDFParser.java:886)
      at org.apache.pdfbox.pdmodel.PDDocument.loadNonSeq(PDDocument.java:1273)
      at org.apache.pdfbox.pdmodel.PDDocument.loadNonSeq(PDDocument.java:1256)
      at *********************.convertPDFToImages(Processor.java:99)
      at ********************.APIProcessor.run(APIProcessor.java:62)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>Also, the first Java heap space error occurred at id22, and the second one is at id33. 
  I am using 8GB RAM on server with <code>MAVEN_OPTS=-d64 -Xms500M -Xmx7000M -XX:PermSize=65M -XX:MaxPermSize=512M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=C:\Temp\heapDump.txt -Xdebug -Xrunjdwp:transport=dt_socket,address=8123,server=y,suspend=n</code></p>
  
  <p>May I ask for some help on what is wrong with my code? Or what is causing the problem here? Is there a memory leak? </p>
  
  <p>P.S.: Here is the link to the rendered pdf file and screen shots of JConsole:
  <a href="https://www.dropbox.com/sh/86gxnmnk5gl66k6/AAAFE_lapBr3cH8EMlXu94qJa?dl=0" rel="nofollow">https://www.dropbox.com/sh/86gxnmnk5gl66k6/AAAFE_lapBr3cH8EMlXu94qJa?dl=0</a></p>
  
  <p>Also, I am now running the program on a 8GB RAM server. But if I run it on my 16GB RAM laptop locally, there is no java heap space error. </p>