HI again,
I am using Aspose 23.1.1 to compress thousands of documents. I am running compression in a thread, and the number of threads is variable.
When I run the program, the memory utilization goes through the roof. I have it tuned to not-quite-reach out of memory, but it gets close.
Here’s the code the compresses a file:
private static void CompressIt(DocumentToStitch docToCompress)
{
var totalPages = docToCompress.Images.Count;
var maxPages = AppSettings.MaxPages;
var isParts = (totalPages > maxPages);
int[] pages = {0,0};
var parts = (totalPages / maxPages);
parts += ((totalPages - (parts * maxPages)) > 0) ? 1 : 0;
for (int i = 0; i < parts; i++)
{
IEnumerable<string> images = new List<string>();
int low = i * maxPages;
int number = Math.Min(maxPages, (totalPages - (i * maxPages)));
pages[0] = low + 1;
pages[1] = low + number;
images = docToCompress.Images.GetRange(low, number);
var optimizeOptions = new Aspose.Pdf.Optimization.OptimizationOptions();
optimizeOptions.ImageCompressionOptions.CompressImages = true;
optimizeOptions.ImageCompressionOptions.ImageQuality = AppSettings.ImageQuality;
optimizeOptions.ImageCompressionOptions.MaxResolution = AppSettings.Resolution;
optimizeOptions.ImageCompressionOptions.ResizeImages = true;
optimizeOptions.ImageCompressionOptions.Version = Aspose.Pdf.Optimization.ImageCompressionVersion.Standard;
optimizeOptions.RemoveUnusedObjects = true;
optimizeOptions.RemoveUnusedStreams = true;
var fileToCompress = docToCompress.OutputPDF;
if (isParts)
{
fileToCompress = fileToCompress.Replace("_Complete", string.Format("_Pages {0:D4} to {1:D4}", pages[0], pages[1]));
}
if (File.Exists(fileToCompress))
{
var outputFile = fileToCompress + ".output.pdf";
try
{
using (var pdfDoc = new Document(fileToCompress))
{
pdfDoc.OptimizeResources(optimizeOptions);
pdfDoc.OptimizeSize = true;
pdfDoc.Save(outputFile);
docToCompress.Compressed = true;
}
File.Delete(fileToCompress);
File.Move(outputFile, fileToCompress, false);
}
catch (Exception e)
{
Logger.LogWrite($"Compression File: {fileToCompress}; DocumentInfo_id: {docToCompress.DocumentInfoId}");
Logger.LogWrite(e.ToString());
return;
}
}
GC.Collect();
}
}
Note that the document may have been broken into 50 page chunks. The documents are just a set of TIF’s or JPG’s stitched together on a another thread. (Right now, I’m not stitching, just compressing.)
Do you see anything that might be a leak?
Thanks,
Bob