The following image shows how RAM usage goes up to 1GB in a very short time, when using aspose.pdf to open a pdf, add a page with a small table, then re-save the pdf file.
RAM usage Aspose Pdf (14.9 KB)
To recreate the issue yourself, please take the following code:
aspose pdf issue code.zip (25.6 KB)
Add a Aspose.PDF.dll in the same folder as the solution file (pdfissue.sln) then start the application, and click the “open pdf” button and select the PROBLEMFILE.pdf which i included in the zip.
Notice how the RAM usage goes to 1GB in about 20 seconds, and the app will eventually crash.
This happens with any version of Aspose.pdf (i’ve tried 9.2.1, 17.4, and also 18.9).
The sample pdf passes as valid pdf version 1.7
Can you please provide a way to safeguard against such drastic memory leaks , ideally in version 17.4 please ? (we have an Aspose.Total for .NET licence)
This is what the code esentially looks like
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
var filepath = openFileDialog1.FileName;
var newFilepath = Path.Combine(
Path.GetDirectoryName(filepath),
Path.GetFileNameWithoutExtension(filepath) + DateTime.Now.ToString("HH-mm-ss") + ".pdf"
);
using (var document = new Aspose.Pdf.Document(filepath))
{
var page = document.AddEmptyPage();
var table = page.Table();
table.Put("Practice", FontStyles.Bold);
document.Save(newFilepath);
}
}
}
}
public static class AsposePdfExtensions
{
public static Page AddEmptyPage(this Aspose.Pdf.Document document)
{
var page = document.Pages.Add();
page.SetPageSize(PageSize.A4.Width, PageSize.A4.Height);
page.PageInfo.Margin = new MarginInfo(40, 60, 40, 30);
return page;
}
public static Row Put(this Table table, string content, FontStyles style = FontStyles.Regular, float fontSize = 10)
{
var row = table.Rows.Add();
row.MinRowHeight = 15;
return Then(row, content, style, fontSize);
}
public static Row Then(this Row row, string content, FontStyles style = FontStyles.Regular, float fontSize = 10)
{
var text = new TextFragment(content);
text.TextState.FontStyle = style;
text.TextState.FontSize = fontSize;
var cell = new Cell { Paragraphs = new Paragraphs { text } };
row.Cells.Add(cell);
return row;
}
public static Table Table(this Page page, string columnWidths = "180 390")
{
var table = new Table
{
Margin = new MarginInfo(0, 15, 0, 0),
ColumnWidths = columnWidths
};
page.Paragraphs.Add(table);
return table;
}
}
}