Aspose PDF memory questions

Hi,


We have purchased Aspose.PDF Java license and I am currently using Aspose 11 and com.aspose.pdf classes.

I have few questions regarding memory utilization by Aspose.

  • Is there any document or links that you guys provide that explains memory utilization done by Aspose ?

  • I have following case please state when the memory will be freed or what should I do to optimize memory utilization ?
  • I have a table that will be rendered in PDF. The table consists of 5 lakh rows( In some cases there are even more rows.). As holding whole 5 lakh rows in memory is not good so we would like to write the 1000 rows at a time and free the memory. So when i add the 1000 rows into the page or document will the memory be freed ? Or is there any other way that you suggest that will help us to optimize memory.

  • I found that in some cases you suggest to stamp things (eg. watermark). Now say I have Case 2 PDF and want to have some watermark to the PDF, then when I load the PDF in the Document object then is it that all memory required to hold the 5 lakh row will be loaded in the memory ? If No please explain the case and if Yes please suggest an alternative by which we can obtain optimum memory utilization and also have watermark on every page of the PDF.

Hi Saurabh,

Thanks for your inquriy. I am afraid there is no documentation available regarding memory utilization of Aspose.Pdf. As Aspose.Pdf uses system memory for processing instead disk, so performance depends upon the system resources and size/contents of the input file.

In your scenario, I am afraid Aspose.Pdf does not support feature to cache on disk. However you can use MemoryCleaner object to clean the memory. After completing operations with Aspose.Pdf object you can close object with close() or dispose() methods and finally use com.aspose.pdf.MemoryCleaner.clear() method, It clears Aspose.Pdf specific instances and will enables you to effective memory usage.

You can create a PDF document with table data and save it into some stream and free the memory as suggested above, later you can load PDF document from the stream into com.aspose.pdf.Document and watermark the PDF document. However if you encounter some issue in the workflow then please share your sample code, we will look into it and will guide you accordingly.

Furthermore, please note it is recommended that you should call this method only if there is a shortage of available memory. Please find sample code to check memory status.

Runtime rt = Runtime.getRuntime();
long max = rt.maxMemory()/1048576;
long total = rt.totalMemory()/1048576;
long free = rt.freeMemory()/1048576;
long used = total - free;

Please feel free to contact us for any further assistance.

Best Regards,

Hi,


I previously posted a question on link.

The answer stated the use of processParagraphs() method on document.

Now my question is like say after writing creating 1000 rows of table I inserted in page and call the processParagraph() method and then later use com.aspose.pdf.MemoryCleaner.clear() method.

Will this work to clear the memory?

Will I be able to write the rest of rows in the page and then in document ?


Hi Saurabh,


Thanks for sharing the details.

The MemoryClearner.Clear() method is used to remove any unused objects or resources occupied by Document instance. Furthermore, processParagraph(…) method is used to render dynamically generated objects inside PDF, so that before saving the document, objects count can be retrieved.

Please try using our API’s and in the event of any further query, please feel free to contact.

Hi,


I have used following code and verified memory optimization and attached the resultant memory graph.

public static void main(String[] args) {
//Instantiate a table object
Document doc = new Document();
// Add page to PDF document
Page page = doc.getPages().add();
page.getPageInfo().setHeight( PageSize.getA4().getHeight() );
page.getPageInfo().setWidth( PageSize.getA4().getWidth() );
MarginInfo info = new MarginInfo( 15, 15, 15, 15 );
page.getPageInfo().setMargin( info );
for( int i = 0 ; i < 100; i++ ) {
Table table = getNestedTable();
page.getParagraphs().add(table);
doc.processParagraphs();
MemoryCleaner.clear();
}
doc.save( tempPath );
}

private static Table getNestedTable() {
Table outermostTable = new Table();
Row outermostrow = outermostTable.getRows().add();
Cell outermostcell = new Cell();
Table rowTable = new Table();
Row rowTableRow = rowTable.getRows().add();
Cell rowTableCell = new Cell();
Table cellTable = new Table();
Row cellTableRow = cellTable.getRows().add();
Cell cellTableCell = new Cell();
Table dataTable = getDataTable();
cellTableCell.getParagraphs().add( dataTable );
cellTableRow.getCells().add( cellTableCell );
rowTableCell.getParagraphs().add( cellTable );
rowTableRow.getCells().add( rowTableCell );
outermostcell.getParagraphs().add( rowTable );
outermostrow.getCells().add( outermostcell );
return outermostTable;
}

private static Table getDataTable() {
Table table = new Table();
for( int i = 0; i < 1000; i++ ) {
Row row = table.getRows().add();
for( int j = 0; j < 5; j++ ){
Cell cell = getCell( i, j );
row.getCells().add(cell);
}
System.out.println("writing "+i);
}
table.setColumnWidths( "100 100 100 100 100 " );
return table;
}

private static Cell getCell( int i, int j ) {
TextSegment segment = new TextSegment( “This is some sample text” );
TextState state = segment.getTextState();
state.setFont( FontRepository.findFont(“Verdana”) );
state.setForegroundColor( Color.getBlack() );
state.setBackgroundColor( Color.getWheat() );
state.setFontSize( 15f );
TextFragment textFragment = new TextFragment();
textFragment.getSegments().add( segment );
Cell cell = new Cell();
cell.getParagraphs().add( textFragment );
BorderInfo info = new BorderInfo();
GraphInfo ginfo = new GraphInfo();
ginfo.setLineWidth( 2 );
ginfo.setColor( Color.getBlack() );
info.setTop( ginfo );
info.setLeft( ginfo );
info.setRight( ginfo );
info.setBottom( ginfo );
cell.setBorder( info );
MarginInfo mInfo = new MarginInfo( 5, 5, 5, 5 );
cell.setMargin( mInfo );
return cell;
}

The results were heavy and not acceptable.( img - 1000 table after memory cleaner.png)

So I tried another way using following code.
I just changed the main block, rest is the same.

public static void main(String[] args) {
long start = System.currentTimeMillis();
for( int i = 0 ; i < 100; i++ ) {
//Instantiate a table object
Document doc = new Document();
// Add page to PDF document
Page page = doc.getPages().add();
page.getPageInfo().setHeight( PageSize.getA4().getHeight() );
page.getPageInfo().setWidth( PageSize.getA4().getWidth() );
MarginInfo info = new MarginInfo( 15, 15, 15, 15 );
page.getPageInfo().setMargin( info );
System.out.println("executing "+i);
Table table = getNestedTable();
page.getParagraphs().add(table);
doc.processParagraphs();
MemoryCleaner.clear();
String temp = tempPath + i + “.pdf”;
doc.save( temp );
}
long end = System.currentTimeMillis();
System.out.println("Generating took "+(end - start));
start = System.currentTimeMillis();
//Instantiate a table object/*
Document doc = new Document();
for( int i = 0; i < 100; i++ ) {
String temp = tempPath + i + “.pdf”;
Document d1 = new Document( temp );
doc.getPages().add( d1.getPages() );
}
end = System.currentTimeMillis();
System.out.println("Merging took "+(end - start));
doc.save( path );
System.out.println(“Generation complete”);
}

In this way I created 1000 rows table and saved the PDF.
At last when 100 PDF’s were created then I merged all 100 PDF’s in one big PDF.

The memory graph is as follows ( img - multiple pdf solution.png )

The memory sure was optimized, But the time was heavily compromised.
The above main block provided below console output-

Generating took 3478148
Merging took 4713
Generation complete

Can you suggest some way where I can optimize both time and memory ?



SaurabhMayekar:
Hi,

I have used following code and verified memory optimization and attached the resultant memory graph.

public static void main(String[] args) {
//Instantiate a table object
Document doc = new Document();
// Add page to PDF document
Page page = doc.getPages().add();
page.getPageInfo().setHeight( PageSize.getA4().getHeight() );
page.getPageInfo().setWidth( PageSize.getA4().getWidth() );
MarginInfo info = new MarginInfo( 15, 15, 15, 15 );
page.getPageInfo().setMargin( info );
for( int i = 0 ; i < 100; i++ ) {
Table table = getNestedTable();
page.getParagraphs().add(table);
doc.processParagraphs();
MemoryCleaner.clear();
}
doc.save( tempPath );
}

private static Table getNestedTable() {
Table outermostTable = new Table();
Row outermostrow = outermostTable.getRows().add();
Cell outermostcell = new Cell();
Table rowTable = new Table();
Row rowTableRow = rowTable.getRows().add();
Cell rowTableCell = new Cell();
Table cellTable = new Table();
Row cellTableRow = cellTable.getRows().add();
Cell cellTableCell = new Cell();
Table dataTable = getDataTable();
cellTableCell.getParagraphs().add( dataTable );
cellTableRow.getCells().add( cellTableCell );
rowTableCell.getParagraphs().add( cellTable );
rowTableRow.getCells().add( rowTableCell );
outermostcell.getParagraphs().add( rowTable );
outermostrow.getCells().add( outermostcell );
return outermostTable;
}

private static Table getDataTable() {
Table table = new Table();
for( int i = 0; i < 1000; i++ ) {
Row row = table.getRows().add();
for( int j = 0; j < 5; j++ ){
Cell cell = getCell( i, j );
row.getCells().add(cell);
}
System.out.println("writing "+i);
}
table.setColumnWidths( "100 100 100 100 100 " );
return table;
}

private static Cell getCell( int i, int j ) {
TextSegment segment = new TextSegment( “This is some sample text” );
TextState state = segment.getTextState();
state.setFont( FontRepository.findFont(“Verdana”) );
state.setForegroundColor( Color.getBlack() );
state.setBackgroundColor( Color.getWheat() );
state.setFontSize( 15f );
TextFragment textFragment = new TextFragment();
textFragment.getSegments().add( segment );
Cell cell = new Cell();
cell.getParagraphs().add( textFragment );
BorderInfo info = new BorderInfo();
GraphInfo ginfo = new GraphInfo();
ginfo.setLineWidth( 2 );
ginfo.setColor( Color.getBlack() );
info.setTop( ginfo );
info.setLeft( ginfo );
info.setRight( ginfo );
info.setBottom( ginfo );
cell.setBorder( info );
MarginInfo mInfo = new MarginInfo( 5, 5, 5, 5 );
cell.setMargin( mInfo );
return cell;
}

The results were heavy and not acceptable.( img - 1000 table after memory cleaner.png)
Hi Saurabh,

Thanks for sharing the details.

I have tested the scenario and have managed to reproduce the problem that PDF creation process keeps on running and never returns. However I have also noticed memory consumption increase issue.For the sake of correction, I have logged it as PDFNEWJAVA-35607 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

SaurabhMayekar:

The results were heavy and not acceptable.( img - 1000 table after memory cleaner.png)

So I tried another way using following code.
I just changed the main block, rest is the same.

public static void main(String[] args) {
long start = System.currentTimeMillis();
for( int i = 0 ; i < 100; i++ ) {
//Instantiate a table object
Document doc = new Document();
// Add page to PDF document
Page page = doc.getPages().add();
page.getPageInfo().setHeight( PageSize.getA4().getHeight() );
page.getPageInfo().setWidth( PageSize.getA4().getWidth() );
MarginInfo info = new MarginInfo( 15, 15, 15, 15 );
page.getPageInfo().setMargin( info );
System.out.println("executing "+i);
Table table = getNestedTable();
page.getParagraphs().add(table);
doc.processParagraphs();
MemoryCleaner.clear();
String temp = tempPath + i + “.pdf”;
doc.save( temp );
}
long end = System.currentTimeMillis();
System.out.println("Generating took "+(end - start));
start = System.currentTimeMillis();
//Instantiate a table object/*
Document doc = new Document();
for( int i = 0; i < 100; i++ ) {
String temp = tempPath + i + “.pdf”;
Document d1 = new Document( temp );
doc.getPages().add( d1.getPages() );
}
end = System.currentTimeMillis();
System.out.println("Merging took "+(end - start));
doc.save( path );
System.out.println(“Generation complete”);
}

In this way I created 1000 rows table and saved the PDF.
At last when 100 PDF’s were created then I merged all 100 PDF’s in one big PDF.

The memory graph is as follows ( img - multiple pdf solution.png )

The memory sure was optimized, But the time was heavily compromised.
The above main block provided below console output-

Generating took 3478148
Merging took 4713
Generation complete

Can you suggest some way where I can optimize both time and memory ?
Hi Saurabh,

Thanks for your patience.

I have also tested above stated scenario and have observed memory consumption and also have noticed that PDF creation process keeps on executing and never stops. The findings have been associated with earlier reported issue and as soon as we have some further updates, we will let you know.

The issues you have found earlier (filed as PDFJAVA-35607) have been fixed in Aspose.Pdf for Java 17.5.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.