We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Convert Tiff to PDF in java | memory leak in Aspose PDF 21.5

Hello,

We are converting Tiff images to pdf using Aspose PDF Java 21.5 and noticed that there appears to be a memory leak because we get an out of memory exception. If I run the follow code snippet below with a tiff images 1000 times to simulate a 1,000 page document it runs out of memory.

BufferedImage image = ImageIO.read(new File(inputFilePath));
        
 boolean rotateToLandscape = image.getWidth() > image.getHeight();

    Document doc = new Document();
    Page page = doc.getPages().add();

    if (rotateToLandscape) {
        page.getPageInfo().setLandscape(true);
    }

    com.aspose.pdf.Image image1 = new com.aspose.pdf.Image();
    image1.setBufferedImage(image);
    page.getParagraphs().add(image1);

    page.getPageInfo().getMargin().setBottom(0);
    page.getPageInfo().getMargin().setTop(0);
    page.getPageInfo().getMargin().setLeft(0);
    page.getPageInfo().getMargin().setRight(0);

    doc.save(outputFilePath);

Error: java.lang.OutOfMemoryError: Java heap space

@nduncanEpiq

Can you please share the source TIFF file so that we may try to reproduce the same on our end.

@mudassir.fayyaz,

I’ve uploaded the file to https://imgur.com/a/Q9fDkco. However, the error is not specific to a particular tiff file. You can reproduce it with any tiff converting the file 1k, 5k, or 10k times.

@nduncanEpiq

I have been able to reproduce the issue on our end. A ticket with ID PDFJAVA-40715 has been created in our issue tracking system to further investigate the issue on our end. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

1 Like

@mudassir.fayyaz do you have any updates to provide on this?

Aaron

@aweech

We have tested it with jdk-9. Java supports TIFF format only from Java 9 release. If you are trying to use ImageIO for TIFF into older Java version it will give you exception.

Wrapped attached code with loop and get clear memory without any leak. (25 mb only is used after each iteration and this number doesn’t grow)
It also works fine without static memory cleaner and no OOM appeared during our testing, but you can’t see the real free memory because default Garbage collector doesn’t clear memory completely after each iteration:

 for (int i = 0; i<1000; i++) {
            System.out.println("Iteration : " + i );
            System.out.println("===================");

//customer's code here ...

            printMemoryStatus_hard();
        }
//...
public static void printMemoryStatus_hard() {
        printMemoryStatus();
        //Forcing GC run with creating big instance.
        int[] oomArray = null;
        try {
            System.out.println("Cleaning memory...");
            com.aspose.pdf.MemoryCleaner.clearStaticInstances();
            oomArray = new int[Integer.MAX_VALUE - 4];
        } catch (OutOfMemoryError e) {
            //Do nothing
        } finally {
            oomArray = null;
        }
        printMemoryStatus();
    }

    public static void printMemoryStatus() {

        Runtime rt = Runtime.getRuntime();
        long max = rt.maxMemory() / 1048576;
        long total = rt.totalMemory() / 1048576;
        long free = rt.freeMemory() / 1048576;
        long used = total - free;
        long realFree = max - used;
        System.out.println("** Memory status (max / used / free):\n\t" + max + "  /  " + used + "  /  " + realFree);
        System.out.println("\t" + new Date() + "\n");

        System.out.println("Active threadCounts: "+ManagementFactory.getThreadMXBean().getThreadCount());        
    }

Maybe the problem in specific tiff loader or encoder that is used if jdk version is lower than 9. Or we need additional information about system and runtime environment to reproduce the issue.

@mudassir.fayyaz
We are not able to upgrade our java right now. Do you have another idea?

@aweech

I am afraid only these details are available for now. You may upgrade for testing purpose if the issue is fixed and then share your findings.