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

Free Support Forum - aspose.com

Memory issues with Aspose

Hi,

I am using Aspose 9.3.0 and I have seen a lot of Memory issues with Aspose. If my output pdf file is more than 100 MB, I get heap space errors. Currently I have configured 1 gb memory for jvm. In my application I can have files as big as 1 GB. I wanted to confirm if Aspose can even support that. below is the code which takes the maximum memory and time.

All the above code is called in a loop since we have a map of bytearrays, and each of these byte array has mutiple pdf pages.

ByteArrayInputStream dataStream = new ByteArrayInputStream(bytes);
//This gives me the pdf pages in the bytearray which will be merged in one document.
com.aspose.pdf.Document dc = new com.aspose.pdf.Document(dataStream);
//If the bytearray has 1000 pdf pages the below line of code takes a long time
finalDocument.getPages().add(dc.getPages());

//After this we use the following code to add Header and Footer

for(int i=startingIndex; i<= finalDocument.getPages().size(); i++)
{
double yIntend = finalDocument.getPages().get_Item(i).getPageRect(false).getHeight()-20;
//Create Header
com.aspose.pdf.TextBuilder textHeaderBuilder = new com.aspose.pdf.TextBuilder(finalDocument.getPages().get_Item(i));
// append the text fragment to the PDF page.Yintend is for positioning in the pdf page.
textHeaderBuilder.appendText(createPageHeader(“Header text”, yIntend));
//Create Footer

com.aspose.pdf.TextBuilder textBuilder = new com.aspose.pdf.TextBuilder(finalDocument.getPages().get_Item(i));
// append the text fragment to the PDF page
textBuilder.appendText(createPageFooter(“Footer Text”, i));
}
}

//After this we call the code for adding Bookmarks and create TOC at the end. Even if I do not add TOC the above code takes upto 1 GB for a 50MB file.

Can you please guide me if I am doing something wrong or is there a w ay to lower the memory consumption. Earlier we were using itext, we have just migrated to Aspose and we are facing this huge memory issue.

Regards,
Rajeev Mathur

Hi Rajeev,


Thanks for your inquiry. In reference to another memory leak issue, we have recently introduced MemoryCleaner class - that clears aspose.pdf specific instances. It is suggested to use clear() method of MemoryCleaner class at the end of any process as mentioned below. It will improve the memory issue a lot.

…<o:p></o:p>

…<o:p></o:p>

pdfDocument.close();<o:p></o:p>

textAbsorber = null;<o:p></o:p>

com.aspose.pdf.MemoryCleaner.clear();<o:p></o:p>

System.gc();<o:p></o:p>


Please feel free to contact us for any further assistance.


Best Regards,

Hi,

By adding com.aspose.pdf.memoryCleaner.clear() statement to the existing code, it slows down 4 times the original time. If it takes 30 seconds to save a 50MB file, after adding the code it takes more than 4 minutes.

Adding the MemoryCleaner does not help when the output file is more than 200 MB, it throws Heap Space error. Does loading a com.aspose.pdf.Document object from bytearray much slower than loading it from file system. Please let mme know if the flow of the above code is wrong and if that is causing huge memory consumption. Since I will need to generate files more than 1GB.

Regards,

Rajeev Mathur

Hi Rajeev,


Thanks for sharing the details.

I have tested the scenario using Aspose.Pdf for Java 9.3.1 in Eclipse Juno application running over Windows 7 (x64) with JDK 1.7 and as per my observations, when manipulating PDF file of size 67.0 MB, the memory consumption hikes by 500MB and process to stamp the PDF file completes in less than 10 seconds. Can you please share the resource PDF which you are using so that we can test again test the scenario at our end.

[Java]

com.aspose.pdf.Document finalDocument = new com.aspose.pdf.Document(“c:/pdftest/TExtFile_TexttoPDF.pdf”);<o:p></o:p>

// ByteArrayInputStream dataStream = new ByteArrayInputStream(bytes);

//This gives me the pdf pages in the bytearray which will be merged in one document.

com.aspose.pdf.Document dc = new com.aspose.pdf.Document("c:/pdftest/XPSConversionTest.pdf");

//If the bytearray has 1000 pdf pages the below line of code takes a long time

finalDocument.getPages().add(dc.getPages());

com.aspose.pdf.MemoryCleaner.clear();

//After this we use the following code to add Header and Footer

for (int i = 1; i <= finalDocument.getPages().size(); i++)

{

double yIntend = finalDocument.getPages().get_Item(i).getPageRect(false).getHeight() - 20;

//Create Header

com.aspose.pdf.TextBuilder textHeaderBuilder = new com.aspose.pdf.TextBuilder(finalDocument.getPages().get_Item(i));

// append the text fragment to the PDF page.Yintend is for positioning in the pdf page.

textHeaderBuilder.appendText(new com.aspose.pdf.TextFragment("Header text"));

//Create Footer

com.aspose.pdf.TextBuilder textBuilder = new com.aspose.pdf.TextBuilder(finalDocument.getPages().get_Item(i));

// append the text fragment to the PDF page

textBuilder.appendText(new com.aspose.pdf.TextFragment("Header text"));

}

finalDocument.save(“c:/pdftest/TextStampIssue.pdf”);