JAVA-Out Of Memory and java heap space exception

Hi,

I am working on a web application. we have a scenario where we need to display thumbnails of pdf documents.

Here is the code:

	Document pdfDocument = new Document(url);
	// Create stream object to save the output image
	ByteArrayOutputStream imageOutputStream = new ByteArrayOutputStream();
	// Create Resolution object
	Resolution resolution = new Resolution(10);
	// Create BmpDevice object with particular resolution
	BmpDevice bmpDevice = new BmpDevice(resolution);
	// Convert a particular page and save the image to stream
	bmpDevice.process(pdfDocument.getPages().get_Item(pageNo+1), imageOutputStream);
	byte[] pageImage = imageOutputStream.toByteArray();
	String pageImageBase64String = Base64.getEncoder().encodeToString(pageImage);
	// Close the stream
	imageOutputStream.close();
	imageOutputStream.flush();
	pdfDocument.close();
	pdfDocument.dispose();

this returns base64 string to UI.

I am getting out of memory, java heap out of memory exception every time I deal with this particular service. it doesn’t matter what pdf I use it gives OOM. I need to generate thumbnails on fly. please suggest some API to generate Thumbnails without having OOM issues.

Any update on above issue.

@kishantejy12

Thank you for contacting support.

We are unable to reproduce the issue with Aspose.PDF for Java 19.9 so please ensure using latest version. If you still face the problem then please share sample PDF document for our reference.

@Farhan.Raza - My mistake , i should have mention this before.

i have 500 documents. i need to return base64 string of first page of each document looping through 500 documents is giving OOM.

Giving you 500 documents is not a good idea but. you can use any pdf document and loop 500 times to get first page base64 .

Server details :- 16 GB Memory and 2 GB Heap Memory.

@kishantejy12

We have surrounded it in a loop but the error did not reproduce. Kindly share your sample document along with JDK and OS details so that we may investigate further.