Java Heap Space Related Error

Hello Team,

We are facing an error related to ‘Java Heap Space’ while converting images to a searchable PDF.

Please find the detailed stacktrace error below:

java.lang.OutOfMemoryError: Java heap space at com.aspose.pdf.internal.l70u.lI.lI(Unknown Source) at com.aspose.pdf.internal.l70u.lI.lI(Unknown Source) at com.aspose.pdf.internal.l71n.lI.(Unknown Source) at com.aspose.pdf.internal.l71k.l0f.(Unknown Source) at com.aspose.pdf.internal.l74h.lj.lI(Unknown Source) at com.aspose.pdf.internal.l74h.lj.write(Unknown Source) at java.desktop/javax.imageio.ImageWriter.write(ImageWriter.java:595) at com.aspose.pdf.internal.l69f.lI.lI(Unknown Source) at com.aspose.pdf.internal.l69f.lI.lf(Unknown Source) at com.aspose.pdf.internal.l69f.lI.lf(Unknown Source) at com.aspose.pdf.internal.l69f.lI.lI(Unknown Source) at com.aspose.pdf.internal.l66p.lf.lI(Unknown Source) at com.aspose.pdf.internal.l66p.l1v.lI(Unknown Source) at com.aspose.pdf.internal.l66p.l1v.lI(Unknown Source) at com.aspose.pdf.internal.l66p.l1v.lI(Unknown Source) at com.aspose.pdf.internal.l9f.l0v.lI(Unknown Source) at com.aspose.pdf.internal.l9f.l0v.lI(Unknown Source) at com.aspose.pdf.internal.l9f.l0v.lI(Unknown Source) at com.aspose.pdf.ADocument.convert(Unknown Source) at com.aspose.pdf.Document.convert(Unknown Source) at com.stormed.production.ProductionMain.doOcr(ProductionMain.java:262) at com.stormed.production.ProductionMain.(ProductionMain.java:123) at com.stormed.production.common.ProductionBuilder.run(ProductionBuilder.java:25) at com.stormed.proxy.AppRunner.run(AppRunner.java:26) at com.stormed.proxy.ProxyMain.runApp(ProxyMain.java:352) at com.stormed.proxy.ProxyMain.lambda$main$0(ProxyMain.java:140) at com.stormed.proxy.ProxyMain$$Lambda$252/0x00000008407f7c40.call(Unknown Source) at co.elastic.apm.agent.concurrent.JavaConcurrent$CallableLambdaWrapper.call(JavaConcurrent.java:253) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:829)

We are using Java 17 and below are the versions of aspose:
[group: ‘com.aspose’, name: ‘aspose-pdf’, version: ‘23.1’],
[group: ‘com.aspose’, name: ‘aspose-imaging’, version: ‘22.7’]

Could you please help us to identify why the exception occurred? Please reach out if you need any additional information.

@JesseLuna

Would you please try with 23.5 version of the API and try increasing the heap size? Please share sample code and files with us if issue still persists. We will test the scenario in our environment and address it accordingly.

Hi, Asad,

Sorry we didn’t follow-up with code and a sample file previously. We’ve updated to Aspose PDF version 23.8 and are still seeing an issue with Java Heap Space errors. I can provide the stack trace that we’re seeing or you can test it yourself with code similar to this and the attached file. Please let us know if you have any questions.

Thanks, Jerry (a colleague of Jesse)

private void doOcr() {
Document.CallBackGetHocr cbgh = new Document.CallBackGetHocr() {
@Override
public String invoke(BufferedImage bi) {
Path tmpTextPath = Paths.get(OCR_FOLDER + “/temp”);
Path tmpImagePath = null;
try {
String[] cmd = new String[] {OCR_EXE, tmpImagePath.toString(), tmpTextPath.toString(), “hocr”};
Process proc = CommonCmd.exec(cmd);
proc.destroy();

tmpTextPath = Paths.get(tmpTextPath.toString() + ".hocr");
StringBuilder fileContents = new StringBuilder((int) tmpTextPath.toFile().length());
try (Scanner scanner = new Scanner(tmpTextPath.toFile())) {
	String lineSeparator = System.getProperty("line.separator");
	while (scanner.hasNextLine()) {
		String l = scanner.nextLine();
		fileContents.append(l).append(lineSeparator);
	}
}
return fileContents.toString();

} catch (Exception ex) {
ex.printStackTrace();
}

return null;
}
});
String imageFilePath = “/tmp/sample.pdf”;
try (Document doc = new Document(imageFilePath)){
doc.convert(cbgh);
doc.save(imageFilePath);
} catch (Exception e) {
Logger.Error(e);
}
}

Aspose_PDF_JavaHeapSpaceError.zip (6.3 KB)

@jmuth

Can you please also share the JDK Version info as well as the Java Heap Size that you have set in your environment? This would help us in investigating the issue accordingly.

JDK17 and Heap space is set at 4GB, Asad.

@JesseLuna

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFJAVA-43154

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

The issues you have found earlier (filed as PDFJAVA-43154) have been fixed in Aspose.PDF for Java 23.11.