简体   繁体   中英

java.lang.OutOfMemoryError: Java heap space while reading image in apache pdfbox

I am creating a pdf using Apache Pdfbox api. While trying to read image, getting Heap Space issue on the below code:

    PDXObjectImage ximage = null;

    InputStream is1 = new ClassPathResource(appProp.getProperty("fon1")).getInputStream();
    InputStream is2 = new ClassPathResource(appProp.getProperty("font2")).getInputStream();

    font = PDTrueTypeFont.loadTTF(doc,is1);
    font1 = PDTrueTypeFont.loadTTF(doc,is2);
    BufferedImage awtImage = ImageIO.read(new ClassPathResource(appProp.getProperty("Logo")).getInputStream());
    ximage = new PDPixelMap(doc, awtImage);
    float scale = 0.009f;

StackTrace:

java.lang.OutOfMemoryError: Java heap space] with root cause

java.lang.OutOfMemoryError: Java heap space at java.awt.image.DataBufferByte.(DataBufferByte.java:92) at java.awt.image.ComponentSampleModel.createDataBuffer(ComponentSampleModel.java:445) at java.awt.image.Raster.createWritableRaster(Raster.java:941) at javax.imageio.ImageTypeSpecifier.createBufferedImage(ImageTypeSpecifier.java:1074) at javax.imageio.ImageReader.getDestination(ImageReader.java:2892) at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1090)

at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1058)

at javax.imageio.ImageIO.read(ImageIO.java:1462)

at javax.imageio.ImageIO.read(ImageIO.java:1357)

Exception occurred while doing ImageIO.read, but not every-time. It's occurring randomly. Same is working fine while running the code in local machine but it's failing in Dev region.

Can somebody help me in resolving the issue.

Thanks,

I was able to resolve this issue after using the image of lesser size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM