获取java.lang.OutOfMemoryError:使用PDFRender API从pdfFile创建图像(缩略图/图像)时的Java堆空间

时间:2011-12-07 13:07:53

标签: java java-ee out-of-memory

我正在处理需要从PdfFile创建PDFImags的项目,因为我正在使用PDFRendere API。我的程序运行成功,当Pdf文件大小很小,如104 kb,180 kb,421 kb等,但我得到“线程中的异常”Image Fetcher 0“* java.lang.OutOfMemoryError:Java堆空间 *当PdfFile尺寸大约或大于12 mb,13 mb,20 mb时。 我在Windows XP,Windows2003服务器和Linux操作系统上检查它,并且还增加了大约1024 MB的Java堆大小,但我仍然得到 java.lang.OutOfMemoryError:Java堆空间。 我的代码是

    try {

        pdfFileName = pdfFileName.trim();
        if (pdfFileName != null || !(pdfFileName.equals(""))) {
            lastIndexx = pdfFileName.lastIndexOf('.');

            if (lastIndexx < 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 & 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;
}

当我调试程序时,它会在

处抛出内存错误
img = dpage.getImage(
                    //rect.width, rect.height, //width & height
                    width, height,
                    rect, // clip rect
                    null, // null for the ImageObserver
                    true, // fill background with white
                    true // block until drawing is done
                    ); 

我从jsp调用这个方法。所以请告诉我任何可能的建议。 在此先感谢,我为任何错误的原因感到抱歉,因为这是我在这里的第一个问题...如果问错误的话,我会尽量不重复

1 个答案:

答案 0 :(得分:3)

java.lang.OutOfMemoryError至少有两个可能的原因:Java堆空间。

  1. 您的应用程序只需要您提供的更多内存。您可以尝试使用-Xmx参数为java增加内存分配。例如。尝试将-Xmx512m添加到您的java命令行。
  2. 您的应用程序或您使用的库leaks memory。然后,您需要尝试使用众多可用工具中的一种来找出并解决此泄漏的原因。您可以推荐Plumbr,即该领域的新玩家。