Free Support Forum - aspose.com

Multithreading issue PNGDevice

Hi,

we are using Aspose PDF library to convert PDF pages of multiple documents to PNG images within multiple threads (up to 10) using following code:

// Load the input PDF document
using (Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(filePath))
{

// Render the page into a raster image
using (MemoryStream imageStream = new MemoryStream())
{
// Create Resolution object
Resolution res = new Resolution(resolution);
if (format == ImageSaveFormat.PNG)
{
// Create PNG device with specified attributes
PngDevice pngDevice = new PngDevice(res);
// Convert a particular page and save the image to stream
pngDevice.Process(pdfDocument.Pages[pageNo], imageStream);
...

After processing of some large amount of PDF files (about 1 Gb) process is stuck within pngDevice.Process method at 100% CPU load and remains in this state forever (see attached image of stacktrace analyser) .

Please advice on correct usage of library in multithreaded environment. Please also take a look at following link
http://improve.dk/debugging-in-production-part-1-analyzing-100-cpu-usage-using-windbg/
it seems that static Dictionaries used here are not thread safe - unfortunately we cannot analyse problem further on our end.

Thanks & Best Regards
Hi Shai,

Thanks for your inquiry. Are you converting some specific page numbers of PDF document to PNG or all pages? We will appreciate it if you please share your working sample code, so we will test the scenario and provide you information accordingly.

Moreover, please note processing time depends upon the file size/contents and system resources. Aspose.Pdf does not use temporary files for processing but memory. So performance can be suffered due to large file processing.

Furthermore, in reference to multithreading support, please note Aspose.Pdf is multithread safe as long as only one thread works on a document at a time. It is a typical scenario to have one thread working on one document, different threads can safely work on different documents at the same time.

We are sorry for the inconvenience caused.

Best Regards,

Hi Ahmad,


thank you for quick response.

Every PDF document has only one page so only one page is extracted.
Only one thread has access to the document.
There should not be a problem to prepare sample, just continuously run the code I provided in 10 worker threads.

File size of one page PDF document always remains low so it is not a performance issue.

Please investigate the source code of PNGDevice and usage of static members of Dictionary - they are not thread-safe and could have causing the deadlock.



Hi Shai,


Thanks for you feedback. I have tested scenario with a sample PDF document using following code and noticed that CPU usage rises to 100%, it hangs for some time but returns to normal after some time. So I have logged a ticket PDFNEWNET-39309 for further investigation and rectification. We will notify you as soon as it is resolved.

try<o:p></o:p>

{

for (int i = 0; i <10;i++ )

{

Thread t = new Thread(convertpdftopng);

t.Start();

}

Console.WriteLine("conversion is done...");

}

catch (Exception error)

{

Console.WriteLine(error.ToString());

}

public static void convertpdftopng()

{

try

{

using (Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document("E:/data/A1-28 - PRECAST STADIA - UPPER BOWL.pdf"))

{

// Render the page into a raster image

using (MemoryStream imageStream = new MemoryStream())

{

// Create Resolution object

Resolution res = new Resolution(300);

// Create PNG device with specified attributes

PngDevice pngDevice = new PngDevice(res);

// Convert a particular page and save the image to stream

pngDevice.Process(pdfDocument.Pages[1], imageStream);

}

}

}

catch (Exception ex)

{

Console.WriteLine(ex.StackTrace);

}

}

We are sorry for the inconvenience caused.


Best Regards,