pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From paolodirollo@gmail.com <paolodiro...@gmail.com>
Subject Re: "OutOfMemoryError: Java heap space" even when using MemoryUsageSetting.setupTempFileOnly()
Date Thu, 20 Sep 2018 06:55:49 GMT
Hi! 

I have the same issue with some PDF and this is my code.

@Async(value = "asyncExecutor")
	public void createThumbnailForPDF(File filePDF, String jobNumber)
	{
		try
		{
			if (filePDF != null && jobNumber != null && !jobNumber.isEmpty())
			{

				jobNumber = jobNumber.trim();
				long fileSizeInBytes = filePDF.length();
				long fileSizeInKB = fileSizeInBytes / 1024;
				long fileSizeInMB = fileSizeInKB / 1024;
				if (fileSizeInMB > PDF_MAX_SIZE)
				{
					log.error("File too large. PDF_MAX_SIZE: " + PDF_MAX_SIZE + ". Thumbnail not created.
Jobnumber: " + jobNumber);
					return;
				}

				try
				{
					String fileName = filePDF.getName();
					String extension = fileName.substring(fileName.lastIndexOf("."));
					if (!StringUtils.isEmpty(extension) && extension.equalsIgnoreCase(".pdf"))
					{
						PDDocument documentPDF = null;
						PDFRenderer pdfRenderer = null;
						BufferedImage bim = null;
						try
						{
							documentPDF = PDDocument.load(filePDF);
							if (documentPDF != null)
							{

								try
								{
									log.info("PDF document loaded. Jobnumber: " + jobNumber + ". PDF Title: " + documentPDF.getDocumentInformation().getTitle());
								} catch (Exception e)
								{
									log.error("Error reading document title property.", e.getMessage());
								}

								pdfRenderer = new PDFRenderer(documentPDF);
								if (pdfRenderer != null)
								{
									bim = pdfRenderer.renderImageWithDPI(0, 78f, ImageType.RGB);
									if (bim != null)
									{
										// thumbnail 200
										int newHeight200 = (int) getNewHeight(bim.getWidth(), bim.getHeight(), 200);
										BufferedImage buff200 = resizeImage(bim, 200, newHeight200);
										if (buff200 != null) ImageIO.write(buff200, "jpg", new File(applicationProperties.getDocumentWithSubfolderRootPath()
+ File.separator + jobNumber + File.separator + jobNumber + "_200.jpg"));

										// thumbnail 100
										int newHeight100 = (int) getNewHeight(bim.getWidth(), bim.getHeight(), 100);
										BufferedImage buff100 = resizeImage(bim, 100, newHeight100);
										if (buff100 != null) ImageIO.write(buff100, "jpg", new File(applicationProperties.getDocumentWithSubfolderRootPath()
+ File.separator + jobNumber + File.separator + jobNumber + "_100.jpg"));

									}
									else
									{
										log.error("Thumbnail creation, Bitmap is null. Jobnumber: " + jobNumber);
									}

								}

							}
							else
								log.error("Thumbnail creation, failed to load PDF. Jobnumber: " + jobNumber);

						} catch (IOException io)
						{
							log.error("Thumbnail creation, IoException while loading PDF. Jobnumber: " + jobNumber,
io.getMessage());
						} catch (Exception e)
						{
							log.error("Failded to create thumbnails. Jobnumber: " + jobNumber, e.getMessage());
						} finally
						{
							try
							{
								if (documentPDF != null) documentPDF.close();
								if (bim != null)
								{
									bim.flush();
									bim = null;
								}
								pdfRenderer = null;

								

							} catch (IOException e)
							{
								log.error("Error while empty the variables after thumbnails creation." + e.getMessage());
							}
						}
					}
					else
					{
						log.info("Current file is not a PDF");
					}

				} catch (Exception e)
				{
					log.error("Failded to create thumbnails. Jobnumber: " + jobNumber, e);
				}
			}

		} catch (Exception e)
		{
			log.error("createThumbnailForPDF something went wrong during thumbnails creation.", e.getMessage());
		}
	}

I think that the code is pretty much correct. So the only solution is to increase the memory
of the JVM?

Regards,
Paolo


On 2016/08/28 11:30:43, Tilman Hausherr <THausherr@t-online.de> wrote: 
> Am 28.08.2016 um 10:53 schrieb Orlev@beeriprint.co.il:
> > looks like MemoryUsageSetting.setupTempFileOnly() has no affect ?
> > do I use it incorrectlly ?
> 
> You're using it correctly. The solution is below:
> 
> >
> > ** I can overcome this problem with -Xmx1500m but had to increase my physical memory..
, and.., I have much bigger files (250000 pages)
> 
> 
> Additionally, if you're doing rendering, take care not to keep the 
> result images in memory.
> 
> 
> Tilman
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Mime
View raw message