Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Mar 08, 2022 4:40 pm

Hi, we have developed a library who generate pdf using Spire.PDF.

We have an issue generating pdf with many pages.

Generate a single pdf file of about 3.000 pages increase the memory during elaboration of 750MB for a resulting pdf size of 13MB.

With 32bit processes some time this result in an out of memory exception.

Can you give me some help to solve the issue?

If you can give me an email address i can send an application to reply the issue.

Thanks

arthurinformatica
 
Posts: 15
Joined: Thu Apr 15, 2021 12:47 pm

Wed Mar 09, 2022 2:10 am

Hello,

Thanks for your inquiry!

According to your describe, you can consider to use "pdf.FileInfo.IncrementalUpdate = false;" to avoid the incremental when add pages. If this method does not work, please provide us with your project via email ([email protected]) for further investigate. Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Wed Mar 09, 2022 9:43 am

Setting pdf.FileInfo.IncrementalUpdate = false seen has no improvements.
I sent you an email with code to reply the issue.

Thanks

arthurinformatica
 
Posts: 15
Joined: Thu Apr 15, 2021 12:47 pm

Wed Mar 09, 2022 9:59 am

Hello,

I have received your email, and thank you for sharing more information.

I will investigate further and feedback you as soon as possible. Hope you can understand.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Thu Mar 10, 2022 10:02 am

Hello,

Thanks for your patience!

After further investigate in your project, I found the reason caused the memory increase and the generated file big.

For the memory increase issue. In your code, you generated the many Font, PdfTrueTypeFont, PdfSolidBrush, PdfPen in each page loop, but did not dispose them after using. This caused the memory these objects use never release and just stacked in memory. The more the pages you generate, the more the memory is consumed.

For the Font object instance from System.Drawing, I just recommend that you can release them after using it by "font.Dispose();". But for the PdfTrueTypeFont, PdfSolidBrush, PdfPen objects from our Spire.PDF, sorry that we do not have the dispose method for them now, but we have the development plan for this. Due to the underlying problem here is very complex, I am afraid that we cannot provide this method for a short period of time. As a temporary method, you can use the following code to save the PDF every 100 pages, release the PdfDocument object, and then reload it. Our product will release the memory of PdfDocument, PdfTrueTypeFont, PdfSolidBrush, PdfPen used each load and close. Here I also attached the compare result about memory use between your original project and the project I changed. You can see that it decrease almost 15% memory use. Since the dispose method is still in developing, I am afraid that this is the better way to decrease the memory use.

Code: Select all
                if (pageNumber % 100 == 0)
                {

                    pdfDoc.SaveToFile(filePath);
                    pdfDoc.Dispose();
                    pdfDoc.Close();
                    pdfDoc = new Spire.Pdf.PdfDocument(filePath);
                    pdfDoc.FileInfo.IncrementalUpdate = false;
                    pdfDoc.FileInfo.Version = (Spire.Pdf.PdfVersion)PdfVersion;
                    pdfDoc.DocumentInformation.Author = "Arthur Informatica s.r.l.";
                    pdfDoc.CompressionLevel = (Spire.Pdf.PdfCompressionLevel)PdfCompressionLevel;
                }


For the generated file size too large issue. Even in eyes see that the generated file is completely empty, but each time the project called page.Canvans.DarwXXX method will add data to the PDF document, and after more than 3000 pages, this size is normal. Hope you can understand.

I also attached the project after change via email, please refer it on your side.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Fri Mar 11, 2022 1:16 pm

Marcia.Zhou wrote:For the Font object instance from System.Drawing, I just recommend that you can release them after using it by "font.Dispose();".


I tried disposing Font and PdfTrueTypeFont but the memory increase is not improved, also, disposing fonts the pdf generation time has increased drastically.

Marcia.Zhou wrote:But for the PdfTrueTypeFont, PdfSolidBrush, PdfPen objects from our Spire.PDF, sorry that we do not have the dispose method for them now, but we have the development plan for this. Due to the underlying problem here is very complex, I am afraid that we cannot provide this method for a short period of time. As a temporary method, you can use the following code to save the PDF every 100 pages, release the PdfDocument object, and then reload it. Our product will release the memory of PdfDocument, PdfTrueTypeFont, PdfSolidBrush, PdfPen used each load and close.


I also tried your code saving pdf document every 100 pages but not has not improved much.

Actually my temporary solution is to implement a "cache" of PdfTrueTypeFont, PdfSolidBrush and PdfPen objects so equal objects can be reused among different draw requests, and before closing the document i clear the cache. Actually in not sure if this solution can be sustainable so i would like to know if you can give me a release date for a fix of this bug.

Thanks

arthurinformatica
 
Posts: 15
Joined: Thu Apr 15, 2021 12:47 pm

Mon Mar 14, 2022 10:17 am

Hello,

Thanks for your feedback.
Our Dev team is doing further investigation to check if there is any approach to do optimization. Sorry at present I can’t tell you when the optimized version can be available. Anyway, once there is any update, we will let you know immediately. Thanks for your patience.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1536
Joined: Wed Apr 25, 2018 3:20 am

Tue Mar 15, 2022 3:39 pm

Hi Lisa,
I send and email with test result using your suggestion, also i added an optiomization on allocating PdfTrueTypeFont / PdfSolidBrush / PdfPen but seem not solve the issue.

If you can check the sample code, i look to some suggenstions for a workaround or additional optiomization i can do on my code.

Thanks

arthurinformatica
 
Posts: 15
Joined: Thu Apr 15, 2021 12:47 pm

Wed Mar 16, 2022 10:28 am

Dear Claudio,

Thanks for your message.
We have given the highest priority to your case. Our Dev team is working on it now. Once there is any update, I will keep you informed at the first time. Thanks for your patience.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1536
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.PDF