A customer sent us this sample code today to monitor memory and time used by a Java thread when using our library jPDFImages to convert and save a PDF page to JPEG. This sample code can be generalized to monitor performance for any Qoppa library.

This code only works for SUN JVMs.

import com.qoppa.pdfImages.PDFImages;
import com.qoppa.util.concurrent.TimeUnit;
import com.sun.management.ThreadMXBean;
try
{
 ThreadMXBean threadMgmt = ((ThreadMXBean) ManagementFactory.getThreadMXBean());
 PDFImages pdfDoc = new PDFImages ("C:\\input.pdf", null);
 for (int count = 0; count < pdfDoc.getPageCount(); ++count)
 {
   long startMem = threadMgmt.getThreadAllocatedBytes(Thread.currentThread().getId());
   long startNanos = System.nanoTime();
   // the process in this sample code is to convert a PDF page to JPG and save it to a file
   pdfDoc.savePageAsJPEG(count, "C:\\output_" + count + ".jpg", 150, 0.8f);
   long endNanos = System.nanoTime();
   long endMem = threadMgmt.getThreadAllocatedBytes(Thread.currentThread().getId());
   System.out.println("Converting and saving page" + count + " at dpi 150 took " + TimeUnit.NANOSECONDS.toMillis(endNanos - startNanos) + " ms and " + (endMem - startMem)/1000000 + " MB");
 }
}
catch (Throwable t)
{
 t.printStackTrace();
}

Here is the output we got when running the code above on a sample PDF:

Converting and saving page 0 at dpi 150 took 2128 ms and 35 MB
Converting and saving page 1 at dpi 150 took 336 ms and 33 MB
...
Converting and saving page 570 at dpi 150 took 149 ms and 27 MB