gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:33:23 25 4
gpt4 key购买 nike

我正在开发一个项目,需要从 PdfFile 创建 PDFImag,为此我正在使用 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 操作系统上检查它,并且还增加了 Java 堆大小约 1024 mb,但我仍然收到 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调用这个方法。所以请指导我任何可能的建议。提前致谢,对于任何错误的原因,我深表歉意,因为这是我的第一个问题...如果我问问题的方式错误,我会尝试不重复

最佳答案

java.lang.OutOfMemoryError: Java heap space 至少有 2 个可能的原因。

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

关于java - 获取 java.lang.OutOfMemoryError : Java heap space when creating images(thumbnails/images) from pdfFile using PDFRender API ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8415791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com