Aspose.Drawing memory leak in Csharp

When I convert pdf to tiff, it takes a lot of ressource. My PDF is 1300K (any pdf does the same) and after 5 calls ended the process still takes 1.7g, even after 5 minutes it’s completion and idle.

Here is my code.

using Aspose.Pdf;
using Aspose.Pdf.Devices;

namespace AsposePdf
{
class Program
{
static void Main(string[] args)
{
Aspose.Pdf.License license = new Aspose.Pdf.License();
license.SetLicense(“Aspose.Pdf.lic”);

        // 5 times : 1.7 Gigs after 5 minutes completion.
        for (int i = 1; i <= 5; i++)
        {
            ProcessPdfToTiff(i);
        }

        license = null;
    }

    static void ProcessPdfToTiff(int iteration)
    {
        Document pdfDocument = null;
        TiffDevice tiffDeviceObj = null;

        using (pdfDocument = new Document("1300KFile.pdf"))
        {
            Resolution resolution = new Resolution(300, 300);
            TiffSettings tiffFileSettings = new TiffSettings
            {
                Compression = CompressionType.LZW,
                Depth = ColorDepth.Format1bpp,
                Shape = ShapeType.None,
                SkipBlankPages = true,
                Brightness = 0.7f
            };

            tiffDeviceObj = new TiffDevice(resolution, tiffFileSettings);
            string dateTimeStr = DateTime.Now.ToString("yyyy-MM-dd_HHmmss");
            string outputFileName = $"AllPagesToTIFF_{dateTimeStr}_iteration{iteration}.tif";
            tiffDeviceObj.Process(pdfDocument, outputFileName);
            System.Console.WriteLine($"Iteration {iteration}: TIFF created - {outputFileName}");

            tiffFileSettings = null;
            resolution = null;
        }
        pdfDocument = null;
    }

}

}

@TheWei789

It looks like you’re experiencing a memory leak when converting PDFs to TIFF using Aspose.PDF. This issue can occur if resources are not properly released after processing.

To resolve this, you should explicitly call the FreeMemory() method on the Document object after processing each PDF. This method helps to clear any memory that the document may still be holding onto.

Here’s how you can modify your ProcessPdfToTiff method:

static void ProcessPdfToTiff(int iteration)
{
    using (Document pdfDocument = new Document("1300KFile.pdf"))
    {
        Resolution resolution = new Resolution(300, 300);
        TiffSettings tiffFileSettings = new TiffSettings
        {
            Compression = CompressionType.LZW,
            Depth = ColorDepth.Format1bpp,
            Shape = ShapeType.None,
            SkipBlankPages = true,
            Brightness = 0.7f
        };

        using (TiffDevice tiffDeviceObj = new TiffDevice(resolution, tiffFileSettings))
        {
            string dateTimeStr = DateTime.Now.ToString("yyyy-MM-dd_HHmmss");
            string outputFileName = $"AllPagesToTIFF_{dateTimeStr}_iteration{iteration}.tif";
            tiffDeviceObj.Process(pdfDocument, outputFileName);
            System.Console.WriteLine($"Iteration {iteration}: TIFF created - {outputFileName}");
        }

        // Free memory after processing
        pdfDocument.FreeMemory();
    }
}

Diagnosis & Next Steps:

  1. Implement the FreeMemory() call after processing the document to ensure that memory is released.
  2. Monitor the memory usage after making this change to confirm that the issue is resolved.

Please try this solution and let me know if it effectively addresses the memory leak!

Sources:
[1]: Document.FreeMemory | Aspose.PDF for .NET API Reference

TiffDevice doesn’t implement iDisposable.

I cant use:
using (TiffDevice tiffDeviceObj = new TiffDevice(resolution, tiffFileSettings))

@TheWei789

Would you please provide (if possible) your sample PDF document along with the screenshot of the memory usage and leak? Also, please confirm if you have tried and tested with the latest version of the API i.e. 25.8.

image.png (52,4 Ko)

version : PackageReference Include=“Aspose.PDF.Drawing” Version=“25.8”

new code with FreeMemory():

using Aspose.Pdf;
using Aspose.Pdf.Devices;

namespace AsposePdf
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 5; i++)
{
ProcessPdfToTiff(i);
}
Console.WriteLine(“Press any key to exit…”);
Console.ReadKey();
}

    static void ProcessPdfToTiff(int iteration)
    {
        Document pdfDocument = null;
        TiffDevice tiffDeviceObj = null;

        pdfDocument = new Document("CATALOGUE.pdf");

        Resolution resolution = new Resolution(300, 300);
        TiffSettings tiffFileSettings = new TiffSettings
        {
            Compression = CompressionType.LZW,
            Depth = ColorDepth.Format1bpp,
            Shape = ShapeType.None,
            SkipBlankPages = true,
            Brightness = 0.7f
        };

        tiffDeviceObj = new TiffDevice(resolution, tiffFileSettings);

        string dateTimeStr = DateTime.Now.ToString("yyyy-MM-dd_HHmmss");
        string outputFileName = $"AllPagesToTIFF_{dateTimeStr}_iteration{iteration}.tif";

        tiffDeviceObj.Process(pdfDocument, outputFileName);
        Console.WriteLine($"Iteration {iteration}: TIFF created - {outputFileName}");

        pdfDocument?.FreeMemory();
        pdfDocument?.Dispose();

        pdfDocument = null;
        tiffDeviceObj = null;

    }
}

}

I made another trial:

image.png (81,2 Ko)
CATALOGUE.pdf (1,0 Mo)

@TheWei789

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-60656

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

I see there is performance issue solved for Aspose.Pdf library. Would it will be solved for Aspose.Drawing as well on version 25.10 ?
Would that solve our memory leak?

@TheWei789

We improve API Performance and Memory Consumption in every monthly release. However, these improvements can be case specific against certain type of functionality and PDF handling. You can try with the latest version if it helps in your situation or not. As far as your dedicated ticket is concerned, we are afraid that investigation against is still pending and and ETA cannot be provided at the moment. As soon as we make some progress towards its resolution, we will update you via this forum thread. Please be patient and spare us some time.

We are sorry for the inconvenience.