title: Java heap space error/ Out of memory error
body: <p>I'm getting heap space error/ out of memory exception.
  I'm trying to generate a PDF using iText and converting the PDF to jpg image using <code>aspose api</code>. The PDF which is being generated is of 3 pages, I'm converting that PDF to image page by page and stitching them together into one jpg image. This code is working fine in my local development machine, but getting exception when move to test server.</p>
  
  <p>The code which I'm using for this is:</p>
  
  <pre><code>public void silSignedPDF(AgreementBean agBean,String sourceTemplatePDFURL, Hashtable&lt;String, String&gt; val, String destinationPDFPath) throws IOException, DocumentException, SQLException{
   String methodName = "silSignedPDF";
  LogTracer.writeDebugLog(className, methodName, "Start");
  String serverPath = System.getProperty("jboss.server.home.dir");
  String sourceTemplatePDFURL1 = serverPath+AppConstants.PDL_Agreement_Template +"/Online_Installment_Agreement.pdf";
  System.out.println("sourceTemplatePDFURL1 "+sourceTemplatePDFURL1);
  
  File f = new File(sourceTemplatePDFURL1);
  InputStream sourceTemplatePDFUrlStream = new BufferedInputStream(new FileInputStream(f));
  File destinationFile = new File(destinationPDFPath+"/"+agBean.getDealNbr()+".pdf");
  PdfReader reader = new PdfReader(sourceTemplatePDFUrlStream);
  PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
          destinationFile));
  AcroFields form = stamper.getAcroFields();
  
  Enumeration enumeration = val.keys();
  // iterate through Hashtable val keys Enumeration
  while (enumeration.hasMoreElements()) {
      String nextElement = (String) enumeration.nextElement();
      String nextElementValue = (String) val.get(nextElement);
      form.setField(nextElement, nextElementValue);
  }
  stamper.setFormFlattening(true);
  stamper.close();
  
  PdfConverter pdf = new PdfConverter();
  pdf.bindPdf(destinationPDFPath+"/"+agBean.getDealNbr()+".pdf");
  try {
      pdf.doConvert();
  } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
  
  //set start and end pages
  pdf.setStartPage(1);
  pdf.setEndPage(1);
  
  //initialize conversion process
  
  //convert pages to images
  String suffix = ".jpg";
  int imageCount = 1;
  while (pdf.hasNextImage())
  {
      try {
          pdf.getNextImage(destinationPDFPath+"/"+agBean.getDealNbr()+"_"+imageCount + suffix,ImageType.JPEG);
      } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
     imageCount++;
  }
  /* PDFImages pdfDoc = new PDFImages (destinationPDFPath+"/"+agBean.getDealNbr()+".pdf", null);
  for (int count = 0; count &lt; pdfDoc.getPageCount(); ++count)
  {
      pdfDoc.savePageAsJPEG(count,destinationPDFPath+"/"+agBean.getDealNbr()+"_"+count + ".png", 150, 0.8f);
  }*/
  File file1 = new File(destinationPDFPath+"/"+agBean.getDealNbr()+"_1" + ".jpg");
  File file2 = new File(destinationPDFPath+"/"+agBean.getDealNbr()+"_2" + ".jpg");
  File file3 = new File(destinationPDFPath+"/"+agBean.getDealNbr()+"_3" + ".jpg");
  
  BufferedImage img1 = ImageIO.read(file1);
  BufferedImage img2 = ImageIO.read(file2);
  BufferedImage img3 = ImageIO.read(file3);
  
  int widthImg1 = img1.getWidth();
  int heightImg1 = img1.getHeight();
  int heightImg2 = img2.getHeight();
  int heightImg3 = img3.getHeight();
  
  BufferedImage img = new BufferedImage(
  widthImg1, 
  heightImg1+heightImg2+heightImg3, 
  BufferedImage.TYPE_INT_RGB);
  
  img.createGraphics().drawImage(img1, 0, 0, null); 
  img.createGraphics().drawImage(img2, 0, heightImg1, null);
  img.createGraphics().drawImage(img3, 0, heightImg1+heightImg2, null);
  
  File final_image = new File(destinationPDFPath+"/"+agBean.getDealNbr() + ".jpg");
  ImageIO.write(img, "png", final_image);
  file1.delete();
  file2.delete();
  file3.delete();
  LogTracer.writeDebugLog(className, methodName, "End");
  }
  </code></pre>