PDF.Kit Memory Usage Issue

Following on from a previous users' issues with a memory leak in the PDF.Kit, a fix was provided from v5.0 onwards I believe.

We have a requirement to generate a thumbnail from the first page of a PDF document to be uploaded into our Document Management System. Unfortunately, attempting to process large .pdf files (we have a 151Mb .pdf test file), results in a large amount of memory usage that doesn't seem to be getting released.

Here are the contents of our own log file.. the memory usage calculators are based on some helpful info provided when we posted a similar problem with the Aspose.Words (in that forum - http://www.aspose.com/community/forums/291344/aspose.words-memory-issue/showthread.aspx#291344 )

Our .pdf upload log is shown below..

** UPLOAD DOCUMENT (Thumbnail) ** Begin Thumbnail Generation
** UPLOAD DOCUMENT (Thumbnail) ** Document Type .pdf is thumbnail capable,
** UPLOAD DOCUMENT (Thumbnail) ** tmpThumbnailFilename = C:\WINDOWS\TEMP\tmp2EB.jpg
** UPLOAD DOCUMENT (Thumbnail) ** set thumbnail creator
** UPLOAD DOCUMENT (Thumbnail) ** call thumbnail creator
** UPLOAD DOCUMENT (Thumbnail) ** Memory Start: 5716992
** UPLOAD DOCUMENT (Thumbnail) ** thumbnail created, now upload
** UPLOAD DOCUMENT (Thumbnail) ** Begin Thumbnail upload to SQL Filestream
** UPLOAD DOCUMENT (Thumbnail) ** clear sqlCommand.Parameters
** UPLOAD DOCUMENT (Thumbnail) ** Begin Thumbnail Upload transaction
** UPLOAD DOCUMENT (Thumbnail) ** Save tmpThumbnail as JPEG
** UPLOAD DOCUMENT (Thumbnail) ** setup DM_InsertThumbnailFileData sp Parameters. DocID = 4760, DocVersionID = 1
** UPLOAD DOCUMENT (Thumbnail) ** executed DM_InsertThumbnailFileData
** UPLOAD DOCUMENT (Thumbnail) ** DataReader has values
** UPLOAD DOCUMENT (Thumbnail) ** loaded DataReader values into local variables and close reader
** UPLOAD DOCUMENT (Thumbnail) ** Start: OpenSQLFilestream into SafeFileHandle
** UPLOAD DOCUMENT (Thumbnail) ** Success: OpenSQLFilestream into SafeFileHandle
** UPLOAD DOCUMENT (Thumbnail) ** Create new Filestream (dbStream2) for SQL Filestream operation
** UPLOAD DOCUMENT (Thumbnail) ** Open new Stream (tmpBmpStream) for tmpThumbnail read operation
** UPLOAD DOCUMENT (Thumbnail) ** Begin: SQL Filestream Write operation
** UPLOAD DOCUMENT (Thumbnail) ** End: SQL Filestream Write operation
** UPLOAD DOCUMENT (Thumbnail) ** Close & Dispose: dbStream2, SafeFileHandle, tmpBmpStream
** UPLOAD DOCUMENT (Thumbnail) ** Commit Thumbnail Upload transaction
** UPLOAD DOCUMENT (Thumbnail) ** Returning Transaction for Thumbnail Upload to calling method
** UPLOAD DOCUMENT (Thumbnail) ** Memory End: 164966680
** UPLOAD DOCUMENT (Thumbnail) ** Memory Used: 159249688 = 151MB

Our PDF thumbnail method looks like this..

public System.Drawing.Bitmap GetThumbNail(string pdfFile) {

MemoryStream ms = new MemoryStream();
PdfConverter converter = new PdfConverter();

//Now we do the licensing stuff, which I have removed for brevity

try {

converter.BindPdf(pdfFile);
converter.DoConvert();

//select the 1st page of the pdf file
converter.GetNextImage(ms, System.Drawing.Imaging.ImageFormat.jpeg);

//create bitmap and point at memory stream
Bitmap bmp = new Bitmap(ms);

//close the memory stream
ms.Close();
ms.Dispose();
converter.Close();
converter = null;
return bmp;

} catch (Exception exc) {
ms.Close();
ms.Dispose();
converter.Close();

throw new Exception(ex.Message);
}

}

When executing this method we see massive memory increase during converter.DoConvert() - which is because the whole .PDF file is being loaded I guess... However after this, is when we do not see any memory released..

For our Words issue, I was dealing with Alexey - I set up a Dropbox share with him.. and could re-use that to provide a copy of the .pdf test file we're seeing issues with..

All help gratefully appreciated :-)

EDIT

Ok, have tested this again but this time included the line:

Settings.Strategy = FileProcessingStrategy.OptimizeMemoryUsage;

And the memory usage now seems to behave itself!! Unfortunately, this puts me back with the problem I posted in the thread below:

http://www.aspose.com/community/forums/294136/pdfconverter.close-not-releasing-handle-on-file/showthread.aspx

Our application does “housekeeping” by cleaning up/deleting any temporary files after the process is completed - however the Aspose.Pdf.Kit component is not releasing all handles on the file so housekeeping fails (please see the above thread for more details)… To get it working, I’m currently NOT deleting temporary .pdf files, but this is not ideal given our requirements…

Hi Marc,

It looks like the memory issue is resolved by using OptimizeMemoryUsage strategy; this leaves us with only one problem mentioned in your other thread (http://www.aspose.com/community/forums/294136/pdfconverter.close-not-releasing-handle-on-file/showthread.aspx). I have replied on that thread and you could follow up over there and share a sample PDF file with us, so we could move forward with the investigation.

We’re sorry for the inconvenience and looking forward to help you out.
Regards,