我對項目工作從pdfFile圖像(縮略圖/圖片),我需要從PdfFile創造PDFImags,對於我使用PDFRendere API時。我的程序運行成功時,PDF文件大小很小,如104 kb,180 kb,421 kb等,但我得到「異常在線程」Image Fetcher 0「* java.lang.OutOfMemoryError:Java堆空間 」 *當PDF文件大小大約或多於12 MB,13 MB,20 MB。 我檢查它在Windows XP,WINDOWS2003服務器,並在Linux操作系統和並增加Java堆的大小約爲1024萬桶,但我仍然得到java.lang.OutOfMemoryError:Java堆空間。 我的代碼得到java.lang.OutOfMemoryError:Java堆空間創建一個使用PDFRender API
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此方法拋出內存不足的錯誤。所以請引導我任何可能的建議。 在此先感謝,我對任何錯誤的原因,因爲它我的第一個問題在這裏對不起...我會盡量不重複,如果要問qustion錯誤的方式
我建議你使用JConsole的檢查自己的JVM的內存分配,看看你是不是以某種方式泄漏對象。這可能是因爲你的代碼不容易讓GC檢索空間,虛擬機中的一個錯誤,或者你只是要求太多... – Romain
感謝你如此之快的答覆,但很明顯,我不知道不了解JConsole以及如何使用它,但我會通過閱讀它來嘗試使用它。但是,如果沒有其他解決辦法則最歡迎和感謝.. –
是的,絕對是最適合你閱讀有關JConsole的 - 這是一個偉大的工具,但在這裏,說明您會採取一個社區的wiki :) – Romain