Huge memory consume

Hi,
if I insert a lot of images into a PDF document, at save the memory consumption rises extremly, and the process is getting slow. Under Linux leads to an out of memory, and the kernel kills the process.
I have made tests with 10-200.000 PNGs, EMFs and SVGs, the size of the PNGs is about 4.5kb, and the used memory was in some cases over 10Gb.
Is it possible somehow save the document stream-like to reduce the memory usage?

I do in the code nothing special, copy-paste from you examples:

        // Initialize document object
        var document = new Document();

        // Add page
        var page = document.Pages.Add();

        var top = 0;
        var left = 0;
        var imageHeight = 100;
        var imageWidth = 100;

        fileInfos.ForEach(FileInfo =>
        {
            var imageStream = new FileStream(FileInfo.FullName, FileMode.Open);
            // Add image to Images collection of Page Resources
            page.Resources.Images.Add(imageStream);

            page.Contents.Add(new GSave());

            // Create Rectangle and Matrix objects
            var rectangle = new Aspose.Pdf.Rectangle(left, page.CropBox.URY - top, imageWidth, page.CropBox.URY - top - imageHeight);
            var matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });

            // Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
            page.Contents.Add(new ConcatenateMatrix(matrix));
            var ximage = page.Resources.Images[page.Resources.Images.Count];

            // Using Do operator: this operator draws image
            page.Contents.Add(new Do(ximage.Name));

            // Using GRestore operator: this operator restores graphics state
            page.Contents.Add(new GRestore());

            top += imageHeight;

            if (page.CropBox.URY - top - imageHeight < 0)
            {
                page = document.Pages.Add();
                top = 0;
            }
        });

        // Save updated PDF
        document.Save("output_Aspose.pdf");

@david.csillik.messerli

Would you kindly share some screenshot of the memory usage at your side. We will further proceed to assist you accordingly. Also, please try using following code snippet to add image inside PDF document:

FileStream mystream = new FileStream(dataDir + "130082451.jpg", FileMode.Open);
Bitmap b = new Bitmap(mystream);
Document doc = new Document();
Page page = doc.Pages.Add();
// Set margins so image will fit, etc.
page.PageInfo.Margin.Bottom = 0;
page.PageInfo.Margin.Top = 0;
page.PageInfo.Margin.Left = 0;
page.PageInfo.Margin.Right = 0;

page.PageInfo.Height = b.Height;
page.PageInfo.Width = b.Width;

// Create an image object
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
page.Paragraphs.Add(image1);
image1.ImageStream = mystream;
doc.Save(dataDir + "2PDF.pdf");

You can also try Adding Image Stamp in PDF File if it helps.

Hi Ali,
Sorry for the late answer, I was ill in the last few days.
The memory usage has these tops with 100.000 small PNGs:
Ellapsed timed: 06:08.37
Peak physical memory usage : 7495Mb
Peak paged memory usage : 7518Mb
Peak virtual memory usage : 2109313Mb
Max virtual memory usage : 2109313Mb
Max private memory usage : 7517Mb
Max working set: 7494Mb
I am testing more different scenarios, because we do not need just a paragraph based text, but also huge tables with a lot of images.
Yes, I know Aspose.PDF supports tables too, but it has the same performance problem. We calculate with 5000+ pages long documents on older PCs, that is why the memory consumption is so important.

@david.csillik.messerli

Thanks for getting back to us.

We really hope that you are in good health now. You may please take your time to test further scenarios and once you get your results, please try to provide a sample console application. We will try to use it at our side with sample PDF document and observe the issue that you are facing. We will surely address it accordingly once replicated at our side.

Hi Ali,
in the main project there is a directory “SampleImages”, and it contains a txt file too. With its content you can make a lot of copy from the sample images, and after that just start the program with the following arguments:
“-l aspose -i [InputDirectoryWithTheImages] -c 100000 -f png”
I hope it helps to figure out what consumes the memory!

@david.csillik.messerli

It seems like a part of code in your shared project is corrupted (please check in the attached screenshot). Would you please that part of code in text format with us.

Also, we have deleted the project shared by you as it contained your license. Please do not share your license unless it is requested explicitly.
codesnippet.png (10.1 KB)