Java aspose for imaging: Converting Tiff files takes long & causes memory issues

Hi,
We have a use case where we have to convert a bunch of documents per request to pdf so our users can view them on the browser without downloading them. In a user session we can have about 5 to 40 documents, and these files include word, images, tiffs (scanned docs), html, pdfs etc.

The other file formats take a reasonable amount of time to convert for our use case (500 milliseconds to 2 secods), the issue is tiff files, they take about 15 seconds to about 50 seconds (or even more for multipage tiffs). This is not ideal for our use case, and they also cause a huge memory strain on our server such that we have to restart our porduction server for about 4 times a day.

For this, We are using aspose-imaging-20.11 for this, running in tomcat8, java jdk8. I have attached a tiff file that takes about 15 seconds to convert as well as the code we are using for the conversion. Could you please help.
Please note we are doing a lot of resource clean up to try and deal with the memory issues.CurrentDocument_02122020_125745.zip (5.0 MB)

final Document doc = new Document();
ByteArrayOutputStream output = null;
InputStream imageInputStream = null;
Page page;
try (InputStream inputStream = new ByteArrayInputStream(input); com.aspose.imaging.Image imagingImage = com.aspose.imaging.Image
  .load(inputStream)) {

  page = doc.getPages().add();
  page.getPageInfo().setMargin(new MarginInfo());
  page.setPageSize(imagingImage.getWidth(), imagingImage.getHeight());

  final Image pdfImage = new Image();
  imageInputStream = new ByteArrayInputStream(input);
  pdfImage.setImageStream(imageInputStream);

  page.getParagraphs().add(pdfImage);

  output = new ByteArrayOutputStream();
  doc.save(output, SaveFormat.Pdf); 


} catch (ImageLoadException | IOException e) {

  return new byte[]{};
} finally {
  if (Objects.nonNull(imageInputStream)) {
    try {
      imageInputStream.close();
    } catch (IOException e) {
      //logger.error(e.getMessage(), e);
    }
  }
  if (Objects.nonNull(output)) {
    try {
      output.close();
    } catch (IOException e) {
      //logger.error(e.getMessage(), e);
    }
  }
  doc.dispose();
  doc.close();
}

@apmanze

I have created an investigation ticket with ID IMAGINGJAVA-1986 in our issue tracking system to further investigate the issue w.r.t performance of the API. We will share the feedback with you as soon as the investigation will be completed on our end.

i will try to do this…

1 Like

@apmanze

Thank you for your understanding.

Depending on the amount of memory assigned to your Java process, and depending on when Garbage Collection kicks in, your process might indeed actually accumulate a lot of used memory. mc-d.uno

@Mohitvermaji51

This issue is under investigation and we will be able to share the feedback with you as soon as the investigation will be completed.

@apmanze

We have worked over the issue. of this issue. The code uses both Aspose.Imaging and Aspose.PDF and we tried to assess this using Aspose.Imaging API. In your example code the following portion have only utilized Aspose.Imaging API.

com.aspose.imaging.Image imagingImage = com.aspose.imaging.Image.load(inputStream);
imagingImage.getWidth(); 
imagingImage.getHeight();

However, rest of rendering has been performed most of work performs in probably Aspose.Pdf or Aspose.Words. In our suggestion as far Aspose.Imaging is concerned, there seems no issue while loading Tiff and if there is any performance related concern that is likely to in part of code where other API has been referenced.

We suggest you to please consider using following modified sample code.

int imgWidth, imgHeight;

// This only part uses Aspose.Imaging and works very fast. But it could work slow if "input" is very large
try (InputStream inputStream = new ByteArrayInputStream(input); 
    com.aspose.imaging.Image imagingImage = com.aspose.imaging.Image.load(inputStream)) 
{

 imgWidth = imagingImage.getWidth();
 imgHeight = imagingImage.getHeight();

} catch (ImageLoadException | IOException e) {
  return new byte[]{};
} finally {
  if (Objects.nonNull(imageInputStream)) {
    try {
      imageInputStream.close();
    } catch (IOException e) {
      //logger.error(e.getMessage(), e);
    }
  }
}

// This part uses Aspose.Pdf and may be a cause of slow working.

final Document doc = new Document();
ByteArrayOutputStream output = null;
InputStream imageInputStream = null;
Page page;

try {  
  page = doc.getPages().add();
  page.getPageInfo().setMargin(new MarginInfo());
  page.setPageSize(imgWidth, imgHeight);

  final Image pdfImage = new Image();
  imageInputStream = new ByteArrayInputStream(input);
  pdfImage.setImageStream(imageInputStream);

  page.getParagraphs().add(pdfImage);

  output = new ByteArrayOutputStream();
  doc.save(output, SaveFormat.Pdf); 


  doc.dispose();
  doc.close();
} finally {
  if (Objects.nonNull(output)) {
    try {
      output.close();
    } catch (IOException e) {
      //logger.error(e.getMessage(), e);
    }
  }
}