JVM Crash when using Aspose-Words and Aspose-PDF

JVM Crash when using both Aspose-Words and Aspose-PDF on 15,000+ page RTFs. There is no problem using Words and PDF separately when working with many large files. Problem exists in all 25.* versions. I am using Java 8 on Windows 64 bit versions. The workflow that produces the problem is as follows, with example code below:

  1. Step 1: convert RTF to PDF by using save method
  2. Step 2: combine PDF (from step1) into another PDF
import com.aspose.pdf.MemoryExtender;
import com.aspose.pdf.PageMode;
import com.aspose.words.*;

// This will crash the JVM when input RTF is 15,000+ pages and using Java 8.  Java 11 seems to avoid this issue.
// This code is a simple way to reproduce the problem of using both Aspose Words save method and Aspose PDF
// * There is no issue using Words or PDF separately with very large input files.  The JVM crashes when both are used
// * The problem exists in all 25.* versions of Words and PDF
// * The JVM Crash occurs in PDF
// * hs_err_pid file always shows
//   Current CompileTask:
//   C2: 579636 11965       4       com.aspose.pdf.internal.l10t.lk::lb (179 bytes)
//
public class SimpleIssue3 {

    public static void main(String[] args) throws Exception {

        // a large input RTF file.  15000+ pages
        String input = args[0];
        // The RTF converted to a PDF in step 1 below
        String convertedPdf = args[1];
        // The PDF created in step 1 combined to a PDF
        String combined = args[2];

        com.aspose.pdf.License lic_pdf = new com.aspose.pdf.License();
        lic_pdf.setLicense("Aspose.PDF.Java.lic");

        com.aspose.words.License lic_words = new com.aspose.words.License();
        lic_words.setLicense("Aspose.Words.Java.lic");

        // Step 1: convert to PDF
        // There is no problem running only this step in a loop to convert many RTFs/Docx inputs into
        // an individual PDF
        LoadOptions loadOptions = new LoadOptions();
        loadOptions.setMswVersion(MsWordVersion.WORD_2007);
        loadOptions.setLoadFormat(LoadFormat.AUTO);
        loadOptions.setTempFolder(tmpFolder);

        com.aspose.words.Document doc = new com.aspose.words.Document(input, loadOptions);
        doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2000);

        PdfSaveOptions pdfOptions = new PdfSaveOptions();
        pdfOptions.setEmbedFullFonts(false);
        pdfOptions.setPreserveFormFields(false);
        pdfOptions.setFontEmbeddingMode(PdfFontEmbeddingMode.EMBED_ALL);
        pdfOptions.setExportDocumentStructure(false);

        pdfOptions.setSaveFormat(SaveFormat.PDF);
        pdfOptions.setMemoryOptimization(true);
        pdfOptions.setTempFolder(tmpFolder);

        doc.save(convertedPdf, pdfOptions);
        System.out.println("Conversion completed");

        // Step 2: combine PDF created in Step 1 to another PDF
        // JVM Crash when used in combination with Step 1 above
        //
        // There is no problem running only this step in a loop to combine many PDF inputs into
        // a single PDF
/
        System.out.println("Opening combined doc");
        com.aspose.pdf.Document combinedPdf = new com.aspose.pdf.Document();
        System.out.println("Loading converted rtf");
        // failure occurs here when Aspose Words save method is called.  No failure when not using Aspose Words
        com.aspose.pdf.Document inputPdf = new com.aspose.pdf.Document(convertedPdf);

        System.out.println("adding pages to combined");
        combinedPdf.getPages().add(inputPdf.getPages());
        combinedPdf.setPageMode(PageMode.UseOutlines);
        combinedPdf.optimize();
        System.out.println("Saving");
        combinedPdf.save(combined);

    }

}


@ssol2

It seems you are experiencing a JVM crash when using both Aspose.Words and Aspose.PDF together on large RTF files, specifically those with over 15,000 pages. This issue appears to occur during the process of converting RTF to PDF and then combining the resulting PDF with another PDF document.

Here are a few suggestions to help you troubleshoot and potentially resolve the issue:

  1. Memory Management: Since you are dealing with very large files, ensure that your Java Virtual Machine (JVM) has sufficient memory allocated. You can increase the heap size by using the -Xmx option when starting your Java application. For example:

    java -Xmx2048m -jar your-application.jar
    
  2. Java Version: You mentioned that the issue does not occur when using Java 11. If possible, consider upgrading your Java version to 11 or later, as it may provide better memory management and performance improvements that could help avoid crashes.

  3. Aspose Library Versions: Ensure that you are using the latest versions of Aspose.Words and Aspose.PDF. Sometimes, bugs are fixed in newer releases, so updating to the latest version might resolve the issue.

  4. Isolate the Problem: Since you noted that the crash occurs when both libraries are used together, try to isolate the problem further. For instance, run the conversion and combination steps separately to see if the crash occurs in a specific part of the process.

  5. Error Logs: Review the hs_err_pid file generated during the crash for more detailed information about what might be causing the JVM to crash. This file can provide insights into the state of the JVM at the time of the crash.

  6. Contact Support: If the problem persists, consider reaching out to Aspose support with detailed information about your environment, the specific versions of the libraries you are using, and the error logs. They may have additional insights or solutions for your specific case.

If you have any further questions or need additional assistance, feel free to ask!

Have tried all of these but does not resolve the issue

@ssol2

Can you please share the last version of Aspose.PDF with which above program was running without any issues?

Has never run without issue as this is newly developed. Have tried with all 25.* versions. Sometimes the code above will run without issue but most of the time the JVM will crash.

@ssol2

Would you kindly share one if your sample input files with along with some information if some error is also showing in console? Also, please share what is the size if installed RAM as well as Java Heap in the system.

I am unable to share an input document but the problem can be replicated when the RTF input file is 15,000+ pages. There is 125+GB of memory and I am nowhere near using that amount when profiling with VisualVM. The input RTF is about 65MB.

@ssol2

Thanks for sharing this information. One more thing, so with RTF file having this amount of pages, you are able to generate PDF using Aspose.Words and JVM Crashes when Aspose.PDF comes into play. Right?

Yes that is correct. If I run the Apose.PDF portion separately then there is no issue.
Attached below is code that generates an RTF that triggers the issue in case this helps.

import com.aspose.words.*;

public class GenerateRTF {


    public static void main(String[] args) throws Exception {

        String dataDir = <update location>;
        // create 15 000 page rtf
        // Load the document.
        Document doc = new Document();

        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.getPageSetup().setPaperSize(PaperSize.LETTER);
        builder.getPageSetup().setOrientation(Orientation.LANDSCAPE);

        for (int i = 0; i < 15000; i++) {

            if (i > 0)
                builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);

            for (int j = 0; j < 5 ; j++) {

                builder.write("Lorem ipsum dolor sit amet consectetur adipiscing elit. Quisque faucibus ex sapien " +
                    "vitae pellentesque sem placerat. In id cursus mi pretium tellus duis convallis. Tempus leo eu " +
                    "aenean sed diam urna tempor. Pulvinar vivamus fringilla lacus nec metus bibendum egestas. " +
                    "Iaculis massa nisl malesuada lacinia integer nunc posuere. Ut hendrerit semper vel class aptent " +
                    "taciti sociosqu. Ad litora torquent per conubia nostra inceptos himenaeos.\n\n");
            }

        }

        doc.save(dataDir + "15000_page.rtf");
    }
}

@ssol2

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-44923

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.