Arvind Gangwar
Arvind Gangwar

Reputation: 153

getting java.lang.OutOfMemoryError: Java heap space when creating images(thumbnails/images) from pdfFile using PDFRender API

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 "Exception in thread "Image Fetcher 0" *java.lang.OutOfMemoryError: Java heap space "* 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 java.lang.OutOfMemoryError: Java heap space. My code is as

    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;
}

when i debug program it throw out of memory error at

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
                    ); 

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

Upvotes: 0

Views: 4069

Answers (1)

Nikem
Nikem

Reputation: 5784

There are at least 2 possible reasons for java.lang.OutOfMemoryError: Java heap space.

  1. Your application just needs more memory that you provide it. You can try to increase the memory allocation for it using -Xmx parameter to the java. E.g. try add -Xmx512m to your java command line.
  2. Your application or one the libraries that you use leaks memory. Then you need to try one of the many tools available to find out and fix the cause of this leak. You can recommend Plumbr, the new player in the field.

Upvotes: 3

Related Questions