Image.Save() Memory Leak C#

I ran into a situation where I had to reduce the size of my tiff image when converting to pdf. I used the code below.

using System;
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.ImageOptions;

namespace TestImaging
{
    public class TiffToPDF
    {
        public static void ConvertTiffToPDF()
        {
            // Applying product license to convert Tiff to PDF in C#
            License TiffToPdfLicense = new License();
            TiffToPdfLicense.SetLicense("Aspose.Total.lic");

            using (Image TifImage = Image.Load("Original.tif"))
            {
                TiffImage tiffImage = (TiffImage)TifImage;
                PdfOptions pdfOptions = new PdfOptions()
                {
                    ResolutionSettings = new ResolutionSetting(
                        tiffImage.HorizontalResolution, tiffImage.VerticalResolution
                        )
                };

                TifImage.Save("ExportedTiff.pdf", pdfOptions);
            }
        }
    }
}

TifImage.Save() caused a major memory leak in my application.
Sharing this info incase anyone else runs into this issue.
Source: How to Convert TIFF to PDF in C#

@scoutd , we will review your request and and give a feedback shortly!

Hello, @scoutd ,
Could you give some additional info on how do you track the memory leak for the code example above?

Hi Denis,
I solved the issue on my end by controlling the buffer size. I added this line of code when loading my image.
new Aspose.Imaging.LoadOptions { BufferSizeHint = 1000 }
By not specifying the buffer size it was unbounded causing the virtual memory to grow out of hand.

Example:

using System;
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.ImageOptions;

namespace TestImaging
{
    public class TiffToPDF
    {
        public static void ConvertTiffToPDF()
        {
            // Applying product license to convert Tiff to PDF in C#
            License TiffToPdfLicense = new License();
            TiffToPdfLicense.SetLicense("Aspose.Total.lic");
            //BufferSizeHint in MB
            using (Image TifImage = Image.Load(streamToConvert, new Aspose.Imaging.LoadOptions { BufferSizeHint = 1000 }))
            {
                TiffImage tiffImage = (TiffImage)TifImage;
                PdfOptions pdfOptions = new PdfOptions()
                {
                    ResolutionSettings = new ResolutionSetting(
                        tiffImage.HorizontalResolution, tiffImage.VerticalResolution
                        )
                };

                TifImage.Save("ExportedTiff.pdf", pdfOptions);
            }
        }
    }
}

Source:

@scoutd , we are pleased you have found the solution using Aspose.Imaging API reference.
Best regards!

1 Like