title: getting java.lang.OutOfMemoryError: Java heap space when creating images(thumbnails/images) from pdfFile using PDFRender API
body: <p>I am working on project where I need to creating PDFImags from PdfFile, for that i am using PDFRendere API. my program running successfully when Pdf File Size is small like 104 kb, 180 kb, 421 kb etc, but I am getting <strong>"Exception in thread "Image Fetcher 0" *<em>java.lang.OutOfMemoryError: Java heap space</strong>
  "</em>* when PdfFile size approx  or more then 12 mb, 13 mb, 20 mb.
  I check it on windows XP, windows2003 server, and on linux OS and And also increase java heap size approx 1024 mb but I am still getting   <strong>java.lang.OutOfMemoryError: Java heap space</strong>.
  My code is as</p>
  
  <pre><code>    try {
  
          pdfFileName = pdfFileName.trim();
          if (pdfFileName != null || !(pdfFileName.equals(""))) {
              lastIndexx = pdfFileName.lastIndexOf('.');
  
              if (lastIndexx &lt; 0) // if extension not present, concatenate the extension
              {
                  filePath = pdfPath+ pdfFileName + ".pdf";
  
              } else {
                  filePath = pdfPath+ pdfFileName;
                  pdfFileName = pdfFileName.substring(0, lastIndexx);
              }
              System.out.println("Pdf system's  redefine physical path, where pdfFile are stored, from PdfController.java : " +filePath);
              file = new File(filePath);
              raf = new RandomAccessFile(file, "r");
              channel = raf.getChannel();
              byteBuf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
              System.out.println("PdfFile Name before Trim() :" + pdfFileName + ":");
              // get a pdf file when pdf is password protected.
              byte[] bytePassword = pdfFileName.trim()).getBytes();               
              pdfPassword = new PDFPassword(bytePassword); 
              pdffile = new PDFFile(byteBuf, pdfPassword);
  
              // draw the image of particular page
              dpage = pdffile.getPage(pageNo);              
  
              //get the width and height for the doc at the default zoom
              rect = new Rectangle(0, 0, (int) dpage.getBBox().getWidth(), (int) dpage.getBBox().getHeight());
              //Rectangle rect = new Rectangle(0,0, 200, 300);
  
              //BufferedImage bufferedImage = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB);
              bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);                
              //generate the image
              img = dpage.getImage(
                      //rect.width, rect.height, //width &amp; height
                      width, height,                 // hardcoded in jsp
                      rect, // clip rect
                      null, // null for the ImageObserver
                      true, // fill background with white
                      true // block until drawing is done
                      );                
              bufImageGraphics = bufferedImage.createGraphics();                
              bufImageGraphics.drawImage(img, 0, 0, null); 
              ImageIO.write(bufferedImage, "jpg", new File(directoryName + "//" + pdfFileName + "_" + pageNo + ".jpg"));
                      succ = true;
  
      } /*catch (OutOfMemoryError ex) {
          System.out.println("From showPdfMetaData.jsp, OutOfMemoryError, Message : " + ex.getMessage());
          System.out.println("From showPdfMetaData.jsp, OutOfMemoryError, Message : " + ex.getCause());
          //errorType = "Jvm throw OutOfMemoryError";
     }*/catch (MethodNotFoundException ex) {
          System.out.println("From pdfToImage(), MethodNotFoundException, Message : " + ex.getMessage());
          System.out.println("From pdfToImage(), MethodNotFoundException, Message : " + ex.getCause());
          System.out.println("pdfFileName, from pdfToImage(), MethodNotFoundException: " + pdfFileName);
          succ = false;
      } catch (StringIndexOutOfBoundsException e) {
          System.out.println("from pdfToImage(), StringIndexOutOfBoundsException !!!");
          System.out.println("pdfFileName, from pdfToImage(), StringIndexOutOfBoundsException: " + pdfFileName);
          e.printStackTrace();
          succ = false;
      } catch (FileNotFoundException e) {
          System.out.println("from pdfToImage(), FileNotFoundException !!!");
          System.out.println("pdfFileName, from pdfToImage(), FileNotFoundException: " + pdfFileName);
          e.printStackTrace();
          succ = false;
      } catch (IOException e) {
          System.out.println("from pdfToImage(), IOException !!!");
          System.out.println("pdfFileName, from pdfToImage(), IOException: " + pdfFileName);
          e.printStackTrace();
          succ = false;
      } catch (NullPointerException e) {
          System.out.println("from pdfToImage(), NullPointerException !!!");
          System.out.println("pdfFileName, from pdfToImage(), NullPointerException: " + pdfFileName);
          e.printStackTrace();
          succ = false;
      } catch (Exception e) {
          System.out.println("from pdfToImage(), General Exception !!!");
          System.out.println("pdfFileName, from pdfToImage(), Exception: " + pdfFileName);
          e.printStackTrace();
          succ = false;
      } finally {
          if (raf != null) {                
              byteBuf.clear();
              channel.close();
              raf.close();
          }
          if(bufferedImage !=null){
             bufferedImage.flush();
             //bufImageGraphics.finalize();
          }
          if(bufImageGraphics !=null){
             bufImageGraphics.dispose();
          }
          if(pdfPassword !=null){
             pdfPassword = null;               
          }            
          if(dpage !=null){
             dpage = null;
          }
          if(rect !=null){
             rect = null;
          }if(img !=null){
              img.flush();
             img = null;
          }if(dmsMgmt !=null){
             dmsMgmt = null;
          }
      }
      return succ;
  }
  </code></pre>
  
  <p>when i debug program it throw out of memory error at    </p>
  
  <pre><code>img = dpage.getImage(
                      //rect.width, rect.height, //width &amp; height
                      width, height,
                      rect, // clip rect
                      null, // null for the ImageObserver
                      true, // fill background with white
                      true // block until drawing is done
                      ); 
  </code></pre>
  
  <p>I am calling this method from jsp. So please guid me any poissible suggestion.
  Thanks in advance, I am sorry for any wrong cause as it my first question here...I will try not repeat if am asking qustion wrong way  </p>