java.lang.OutOfMemoryError: Java heap space -Aspose.pdf-17.3.0

Using aspose.pdf (17.3.0), aspose.imaging, and aspose.words in a spring-boot web application. In most of the cases (including aspose.pdf), I am loading a document from a stream, saving a particular page in JPEG format and returning it in response to the requests.

In some cases I am getting the following exception while running with -Xmx512m memory (more frequent while running with -Xmx256m memory).

java.lang.OutOfMemoryError: Java heap space
at com.aspose.pdf.internal.p622.z32.m1(Unknown Source)
at com.aspose.pdf.internal.p622.z32.m3(Unknown Source)
at com.aspose.pdf.internal.p622.z32.write(Unknown Source)
at com.aspose.pdf.internal.p789.z3.m1(Unknown Source)
at com.aspose.pdf.internal.p622.z41.fromJava(Unknown Source)
at com.aspose.pdf.ADocument.(Unknown Source)
at com.aspose.pdf.Document.(Unknown Source)

@kati_19,

It is always recommended to use the latest version of the API. The latest version contains new feature and big fixes reported by other customers. Please share any sample input PDF file that is creating problem along with sample code that you are using. This will help us to reproduce the issue at our end. Furthermore update us about which JDK version are you using.

Usign following code for aspose.pdf:
JDK 1.8

public byte[] getPageData(InputStream inputStream, int pageNumber) {
byte[] bytea = null;
try {
Document doc = new Document(inputStream);
Resolution resolution = new Resolution(Constants.IMAGE_RESOLUTION);
JpegDevice jpegDevice = new JpegDevice(resolution, Constants.IMAGE_QUALITY);
try (ByteArrayOutputStream fos = new ByteArrayOutputStream()) {
jpegDevice.process(doc.getPages().get_Item(pageNumber), fos);
bytea = fos.toByteArray();
}
} catch (IOException e) {
log.error("Error while rendering page number: " + pageNumber, e);
}
return bytea;
}

Have a look please…

@kati_19

Thanks for sharing the sample code snippet.

We have tested the scenario in our environment with one of our sample PDFs and were unable to notice any issue. Please note that we have used following values, for Resolution and Image_Quality objects, as they were undefined in the provided code snippet:

Resolution = 300
Image_Quality = 100

Would you please share your sample PDF, with which you are facing the OutOfMemory Exception, so that we can again test the scenario in our environment and address it accordingly.

I need to test the application again. Then I’ll share related file. By the way, I am setting Resolution = 100 and Image_Quality = 100.
And I also want to know that, is there any possibilities of this issue to be related to dispose() method of the Document object. Because I am not disposing the Document object after use.

Thank you

@kati_19

Thanks for writing back.

The issue is not related to disposing the Document object, as you are disposing it after use. Usually OOM Exception occurs due to heap space error and it depends upon the PDF document that how much memory API is consuming to process the document. Also it will be really helpful if you can please share your environment details (e.g OS Version, Application Type etc.) along with sample PDF file. We will test the scenario in specific environment and let you know about our findings.

  • OS: Ubuntu 16.04
  • JDK 1.8
  • Spring-Boot Web Application with caching
  • Build Tool: Maven
  • Aspose-Pdf:17.3
  • Aspose-Words: 17.4
  • Aspose-cells: 17.3
  • Aspose-slides: 17.3
  • Aspose-Imaging-17.3

Files are quite large(greater than 20MB), so not able to upload here.

And also today I got more than a couple of OOM errors in my JUnit integration testing, one of those is as follows:

*** java.lang.instrument ASSERTION FAILED ***: "!errorOutstanding" with message can't create name string at JPLISAgent.c line: 807
    java.lang.OutOfMemoryError: Java heap space
    	at sun.nio.cs.UTF_8.newDecoder(UTF_8.java:68)
    	at java.lang.StringCoding$StringDecoder.<init>(StringCoding.java:131)
    	at java.lang.StringCoding$StringDecoder.<init>(StringCoding.java:122)
    	at java.lang.StringCoding.decode(StringCoding.java:187)
    	at java.lang.String.<init>(String.java:426)
    	at java.util.jar.Manifest.parseName(Manifest.java:269)
    	at java.util.jar.Manifest.read(Manifest.java:223)
    	at java.util.jar.Manifest.<init>(Manifest.java:69)
    	at java.util.jar.JarFile.getManifestFromReference(JarFile.java:194)
    	at java.util.jar.JarFile.getManifest(JarFile.java:180)
    	at sun.misc.URLClassPath$JarLoader$2.getManifest(URLClassPath.java:981)
    	at java.net.URLClassLoader.defineClass(URLClassLoader.java:450)
    	at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    	at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    	at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    	at com.aspose.slides.p7cce53cf.cj.do(Unknown Source)
    	at com.aspose.slides.p7cce53cf.cj.<clinit>(Unknown Source)
    	at com.aspose.slides.p7cce53cf.aa.if(Unknown Source)
    	at com.aspose.slides.p7cce53cf.aa.do(Unknown Source)
    	at com.aspose.slides.p7cce53cf.aw.for(Unknown Source)
    	at com.aspose.slides.p7cce53cf.i.do(Unknown Source)
    	at com.aspose.slides.p7cce53cf.aw.if(Unknown Source)
    	at com.aspose.slides.p7cce53cf.cs.<init>(Unknown Source)
    	at com.aspose.slides.bfq.do(Unknown Source)
    	at com.aspose.slides.Presentation.new(Unknown Source)
    	at com.aspose.slides.Presentation.for(Unknown Source)
    	at com.aspose.slides.Presentation.if(Unknown Source)

Even I have tried with many files of sizes less than 100KB for testing. I’m getting this in aspose-pdf and aspose-slides.

@kati_19

Thanks for sharing requested information.

We are working over setting up the specified environment, in order to test the scenario. As soon as we test the scenario, we will share our feedback with you. Meanwhile, would you please share, how many concurrent calls, the method (public byte[] getPageData(...)) could receive.

Please share the code snippet related to Aspose.Slides along with sample input/output files, so that we can test this scenario as well, in our environment and address it accordingly.

You can please upload your sample file to some public file sharer (e.g Dropbox, Google Drive) and share the link with us. We will test the scenario with specific document and share our findings with you.

Concurrent calls:
Currently, with each request, an asynchronous method will be called that will fetch and store 4 pages (these 4 calls are sequential) in the cache by calling (public byte[] getPageData(...)).
And currently, I am not trying it with multiple requests simultaneously. But there are concurrent calls to the async method while making sequential requests.
But the method (public byte[] getPageData(...)) is not asynchronous, so… I am not sure but I think this method won’t be executed concurrently.

Code for Aspose.Slides:

   1.         Presentation pres = new Presentation(inputStream);
            ISlide slide = pres.getSlides().get_Item(pageNumber - 1);

            BufferedImage image = slide.getThumbnail(Constants.SCALE_X, Constants.SCALE_Y);
            ImageIO.write(image, "jpeg", bout);

            page = bout.toByteArray();



 
2.        Map<String, String> attributes = new HashMap<>();


            Presentation pres = new Presentation(inputStream);
            IDocumentProperties props = pres.getDocumentProperties();

            attributes.put(Constants.AUTHOR, props.getAuthor());
            attributes.put(Constants.CONTENT_TYPE, props.getContentType());
            attributes.put(Constants.CREATION_DATE, props.getCreatedTime().toString());
            attributes.put(Constants.TITLE, props.getTitle());
            attributes.put(Constants.MODIFIED_DATE, props.getLastSavedTime().toString());
            attributes.put(Constants.PAGE_COUNT, Integer.toString(pres.getSlides().size()));

Other information I’ll be sharing shortly.
Thank you

@kati_19

Thanks for sharing sample code snippet.

Please take your time to share the sample file(s) with us. As shared earlier, we are working setting up the specified environment so that we can test the scenario and try to observe the same issue at our side, which you have mentioned.

Please note, I am not disposing the Document object. Yeah, I have tried that too, but it did not do anything noticeable. Still clear it for me whether disposing of the Document object is required or not!!

@kati_19

Thanks for your inquiry.

Generally it is good practice to dispose an object after it is no longer needed and in case that object uses some resources and they are not being disposed with that object, this leads to memory leaks.

In Aspose.Pdf DOM model, Document.Save(...) method is more like disposing the object itself as well as other resources which were being used by it. In DOM model, you work with a complete document object which contains full hierarchy of classes and it uses many unmanaged resources, so as I said earlier that if resources used by an object, are not disposed with it, it may lead to memory leaks. Which is why this new feature of disposing object with save method was implemented in the API.

Since, you are not disposing object in your code snippet, neither you are using Save(...) method, so it is advisable to dispose the object at the end of its usage. However, you may use try-finally approach (just like using statement in C#.NET) and make sure that all objects are disposed after they are no longer of use.

We have tested the scenario in an environment i.e Ubuntu 15.04, Spring-Boot Application, Maven Build, JDK 1.8 and Aspose.Pdf 17.10 for Java. We were unable to reproduce the OOM exception at our side. It will really be appreciated, if you can please share a sample application, which generates the exception at your end. It will help us in replicating the issue in our environment and address it accordingly.

Please also note that, it is always recommended to use latest version of the API as it contains more fixes and performance improvements. You may also try your scenario with latest version of the API and let us know about the results at your end.

Now concerning to the Aspose.Slides related issue, we are working over testing the scenario and will let you know about our findings as soon as possible. Please spare us little time.

We are sorry for the inconvenience.

@kati_19,

For your following issue concerning to Aspose.Slides:

Can you please try increasing the Java heap size on your end to avoid OutOfMemory Exception. If there is still an issue then can you please share the source presentation files along with sample application project reproducing the issue on your end. If the presentation are big enough then you can share the presentation over some FTP server (like dropbox) and provide download link with us.

@kati_19,

I have worked with the presentation files shared by you using Aspose.Slides for Java 17.10 on my end using Windows 7 x64 and JDK 1.7_79. I have shared the generated output thumbnails for your convenience as well. As shared earlier that in order to avoid OutofMemory exception, please consider increasing Java heap size on your end.

Slides.zip (1.3 MB)

I appreciate your effort guys.
Currently, in the test environment, I have solved this issue by increasing heap size for the testing framework.
I’ll see what more information I can provide you. As soon as I get more results, I’ll share with you.

Thank you very much.

Hi, got the issue again in aspose-slides-17.12 and older versions also.
Corresponding files are attached. Have a look.

Simple Java Application

  • File name: DARE-School-Presentation-2018_Updated.pptx

  • -Xmx256m

  • OutOfMemoryError

  • File name: Real-Estate-Postcard_v3-b83faf.pptx

  • -Xmx512m

  • OutOfMemoryError

    Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
    at java.awt.image.DataBufferByte.(DataBufferByte.java:92)
    at java.awt.image.ComponentSampleModel.createDataBuffer(ComponentSampleModel.java:445)
    at sun.awt.image.ByteInterleavedRaster.(ByteInterleavedRaster.java:90)
    at sun.awt.image.ByteInterleavedRaster.createCompatibleWritableRaster(ByteInterleavedRaster.java:1281)
    at sun.awt.image.ByteInterleavedRaster.createCompatibleWritableRaster(ByteInterleavedRaster.java:1292)
    at com.aspose.slides.internal.fe.int.new(Unknown Source)
    at com.aspose.slides.internal.fe.int.do(Unknown Source)
    at com.aspose.slides.internal.ek.if.do(Unknown Source)
    at com.aspose.slides.internal.ek.if.do(Unknown Source)
    at com.aspose.slides.internal.ek.if.do(Unknown Source)
    at com.aspose.slides.internal.ek.if.do(Unknown Source)
    at com.aspose.slides.internal.ek.if.deepClone(Unknown Source)
    at com.aspose.slides.PPImage.do(Unknown Source)
    at com.aspose.slides.x3.do(Unknown Source)
    at com.aspose.slides.x3.do(Unknown Source)
    at com.aspose.slides.Picture.do(Unknown Source)
    at com.aspose.slides.lq.do(Unknown Source)
    at com.aspose.slides.lq.do(Unknown Source)
    at com.aspose.slides.lq.do(Unknown Source)
    at com.aspose.slides.lq.(Unknown Source)
    at com.aspose.slides.GeometryShape.do(Unknown Source)
    at com.aspose.slides.GeometryShape.do(Unknown Source)
    at com.aspose.slides.PictureFrame.do(Unknown Source)
    at com.aspose.slides.GroupShape.do(Unknown Source)
    at com.aspose.slides.Slide.if(Unknown Source)
    at com.aspose.slides.Slide.do(Unknown Source)
    at com.aspose.slides.Slide.do(Unknown Source)
    at com.aspose.slides.Slide.for(Unknown Source)
    at com.aspose.slides.Slide.getThumbnail(Unknown Source)
    at AsposeSlideTest1007.getPageData(AsposeSlideTest1007.java:33)
    at AsposeSlideTest1007.main(AsposeSlideTest1007.java:20)

spring web application:

  • For both the files

  • -Xmx512m

  • OutOfMemoryError

    java.lang.OutOfMemoryError: Java heap space
    at java.awt.image.DataBufferByte.(DataBufferByte.java:92)
    at java.awt.image.ComponentSampleModel.createDataBuffer(ComponentSampleModel.java:445)
    at sun.awt.image.ByteInterleavedRaster.(ByteInterleavedRaster.java:90)
    at sun.awt.image.ByteInterleavedRaster.createCompatibleWritableRaster(ByteInterleavedRaster.java:1281)
    at sun.awt.image.ByteInterleavedRaster.createCompatibleWritableRaster(ByteInterleavedRaster.java:1292)
    at com.aspose.slides.internal.ei.int.new(Unknown Source)
    at com.aspose.slides.internal.ei.int.do(Unknown Source)
    at com.aspose.slides.internal.dn.if.do(Unknown Source)
    at com.aspose.slides.internal.dn.if.do(Unknown Source)
    at com.aspose.slides.internal.dn.if.do(Unknown Source)
    at com.aspose.slides.internal.dn.if.do(Unknown Source)
    at com.aspose.slides.internal.dn.if.deepClone(Unknown Source)
    at com.aspose.slides.PPImage.do(Unknown Source)
    at com.aspose.slides.xy.do(Unknown Source)
    at com.aspose.slides.xy.do(Unknown Source)
    at com.aspose.slides.Picture.do(Unknown Source)
    at com.aspose.slides.lp.do(Unknown Source)
    at com.aspose.slides.lp.do(Unknown Source)
    at com.aspose.slides.lp.do(Unknown Source)
    at com.aspose.slides.lp.(Unknown Source)
    at com.aspose.slides.GeometryShape.do(Unknown Source)
    at com.aspose.slides.GeometryShape.do(Unknown Source)
    at com.aspose.slides.PictureFrame.do(Unknown Source)
    at com.aspose.slides.GroupShape.do(Unknown Source)
    at com.aspose.slides.Slide.if(Unknown Source)
    at com.aspose.slides.Slide.do(Unknown Source)
    at com.aspose.slides.Slide.do(Unknown Source)
    at com.aspose.slides.Slide.for(Unknown Source)
    at com.aspose.slides.Slide.getThumbnail(Unknown Source)

my code:

    byte[] page = null;
    Presentation pres = null;

    try (ByteArrayOutputStream bout = new ByteArrayOutputStream()) {
        pres = new Presentation(inputStream);
        ISlide slide = pres.getSlides().get_Item(pageNumber - 1);

        BufferedImage image = slide.getThumbnail(0.15f, 0.15f);
        ImageIO.write(image, "jpeg", bout);

        page = bout.toByteArray();
    } catch (IOException e) {
        System.out.println("Error while rendering presentation page, page number: " + pageNumber + e);
    }

    return page;

Real-Estate-Postcard_v3-b83faf.pptx.zip (149.7 KB)

@kati_19,

I have worked with presentation files , Real-Estate-Postcard_v3-b83faf.pptx and DARE-School-Presentation-2018_Updated.pptx on my end. For presentation, DARE-School-Presentation-2018_Updated.pptx, I need to have minimum heap-size 512M. Where as for Real-Estate-Postcard_v3-b83faf.pptx, I have to set heap-size to 1024M. Both presentation files rendered fine to images and for your kind reference, I have attached the generated thumbnails.

Slides.zip (1.4 MB)
Slides2.zip (1.5 MB)

Okay, means I need at least this much (as you told) memory to process these files. Correct me if I am wrong. If you can clear some of my doubts, please.

  • For Aspose APIs to process different files of various sizes, the minimum heap memory requirements vary?

  • It does not completely depends on the size of files (i.e., “if file size increases, heap memory requirements also increases” is not always true)?

  • And if you would like to explain a little, why does it take more memory to process some smaller size files than some bigger size files? (like two files I’ve provided).

Thank you for your response.

@kati_19,
I have observed your following question.

Yes, the heap size depends on presentation, its size, number of slides and content inside that. When presentation gets loaded in Document Object Model (DOM) of Aspose.Slides, all class data structures and statics gets loaded in memory. So, size of presentation, number of slides and content inside presentation directly depends on amount of memory required. Secondly, when you render presentation to image or PDF, additional memory is required for generating required outputs. I hope the shared elaboration will be helpful.