PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 7:40 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Oct 15, 2014 8:37 am 
Offline

Joined: Wed Oct 15, 2014 8:15 am
Posts: 2
Hello,

I work on the creation of a pdf document with no images.
I import lists of points and create dynamically one graphic ( or more ) per pdf document page.
I use XGraphics.DrawLine to build the graphic between each points.
my pdf at the end has a size of 19 to 200 Mo with 430 pages approximately.
After each page creation i dispose my Xgraphic component to free memory but at the end the process uses 1 500 000 Ko.
When i just have one graphic per page until 7 it s ok. When i select 8 files (files exported to get coordinates to build graphics), at th e 337 page, i have an outofmemory exception in the XGraphicsPdfRenderer class in the Close() methode when i pass on this instruction :
content.CreateStream(PdfEncoders.RawEncoding.GetBytes(GetContent()));

I tried to catch the exception to see the result file and after page 337 some of them are completly white (normal case) but 2 or 3 have correctly created the graphic part. Seems like some memory has been freed and could be used.

Could you help me, i can t use a verifying tool cause i can t install it on my computer (admin restrictions) and i think i have disposed all non managed objects.

Here is the exception thrown :
Une exception de type 'System.OutOfMemoryException' a été levée.
à System.String.ToCharArray()
à System.Text.Encoding.GetBytes(String s, Int32 charIndex, Int32 charCount, Byte[] bytes, Int32 byteIndex)
à System.Text.Encoding.GetBytes(String s)
à PdfSharp.Drawing.Pdf.XGraphicsPdfRenderer.Close() dans d:\projets\test_PdfSharp\dll\PDFSharp-MigraDocFoundation-1_32\PDFsharp\code\PdfSharp\PdfSharp.Drawing.Pdf\XGraphicsPdfRenderer.cs:ligne 108

Thanks.
Jon


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 15, 2014 10:26 am 
Offline

Joined: Wed Oct 15, 2014 8:15 am
Posts: 2
Up!

i give you the main part of my project, just to know if there s somethink i didn t do right.

Code:
            while (!docCreated)
            {
                if (tempCount > samples) break; //même distance parcourue pour les mesures d'une même tournée
                PdfPage pdfPage = pdfDoc.AddPage();
                XGraphics gfx = XGraphics.FromPdfPage(pdfPage);
                pdfPage.Size = PageSize.A3;
                pdfPage.Orientation = PageOrientation.Landscape;
               

                //////////Structure de la page pdf//////////
                int minGraph_X = maxPage_X - 60;
                int minGraph_Y = Convert.ToInt32((decimal)(maxPage_Y - 20) / (decimal)Piv_Datas.Count()); // hauteur d'un graphique
                //////////Structure de la page pdf//////////

                for(int indexPivot = 0;indexPivot<Piv_Datas.Length;indexPivot++)
                {
                    double minValue = Piv_Datas[indexPivot].Select(piv => piv.Y).Min();
                    double maxValue = Piv_Datas[indexPivot].Select(piv => piv.Y).Max();
                    double pasMesure_H = Convert.ToDouble((decimal)minGraph_X / (decimal)pointsToSee);
                    double pasMesure_V = Convert.ToDouble((decimal)minGraph_Y / (decimal)(maxValue - minValue));
                    double origineGraph = 10 + (minGraph_Y) * (1 + indexPivot) - (Math.Abs(minValue) * pasMesure_V); // plusieurs origines en fonction du nombre de graphiques

                    //Background
                    XLinearGradientBrush brush = new XLinearGradientBrush(new XRect(maxGraph_X, maxGraph_Y + minGraph_Y * indexPivot, minGraph_X, minGraph_Y), XColors.AliceBlue, XColors.LightBlue, XLinearGradientMode.Vertical);
                    gfx.DrawRectangle(new XPen(XColors.Black, 0.1), brush, new XRect(maxGraph_X, maxGraph_Y + minGraph_Y * indexPivot, minGraph_X, minGraph_Y));
                    //Recouvrement Background
                    XLinearGradientBrush redGradientBrush = new XLinearGradientBrush(new XRect(maxGraph_X, maxGraph_Y + minGraph_Y * indexPivot, minGraph_X, minGraph_Y), XColors.Beige, XColors.LightPink, XLinearGradientMode.Vertical);
                    gfx.DrawRectangle(redGradientBrush, new XRect(maxGraph_X + 1, maxGraph_Y + 1 + minGraph_Y * indexPivot, pointsInRecouvrZone * pasMesure_H - 1, minGraph_Y - 2));
                    gfx.DrawRectangle(redGradientBrush, new XRect(maxPage_X - 11 - pointsInRecouvrZone * pasMesure_H, maxGraph_Y + 1 + minGraph_Y * indexPivot, maxPage_X - (maxPage_X - pointsInRecouvrZone * pasMesure_H), minGraph_Y - 2));

                    // repères valeurs
                    gfx.DrawString(maxValue.ToString("0.00"), new XFont("Verdana", 8, XFontStyle.BoldItalic), XBrushes.Gray, new XPoint(10, maxGraph_Y + 10 + minGraph_Y * indexPivot));
                    gfx.DrawString("0", new XFont("Verdana", 10, XFontStyle.BoldItalic), XBrushes.Gray, new XPoint(40, origineGraph));
                    gfx.DrawString(minValue.ToString("0.00"), new XFont("Verdana", 8, XFontStyle.BoldItalic), XBrushes.Gray, new XPoint(10, minGraph_Y * (indexPivot + 1)));

                    XPen pen = new XPen(XColors.Black, 0.01);
                    int countMax = 0;
                    if (globalTempCount + pointsToSee > samples) // cas en fin de constitution de courbe (dernière page pdf)
                    {
                        countMax = Convert.ToInt32(samples);
                        docCreated = true;
                    }
                    else
                    {
                        if (indexPivot != 0)
                        {
                            tempCount = globalTempCount;
                            countMax = tempCount + pointsToSee;
                        }
                        else
                            countMax = tempCount + pointsToSee;
                    }

                    int xTranslation = 0;
                    for (int indexMeasure = tempCount; indexMeasure < countMax; indexMeasure++)
                    {
                        try
                        {
                            // affichage des points de mesure
                            gfx.DrawLine(pen, xTranslation * pasMesure_H + maxGraph_X,
                                        -Piv_Datas[indexPivot][indexMeasure].Y * pasMesure_V + origineGraph,
                                        (xTranslation + 1) * pasMesure_H + maxGraph_X,
                                        -Piv_Datas[indexPivot][indexMeasure + 1].Y * pasMesure_V + origineGraph);

                            if (indexMeasure != 0 && indexMeasure % repereVerticaux_MeterToPt == 0 && indexPivot == Piv_Datas.Length - 1)
                            {
                                gfx.DrawString(((decimal)indexMeasure / (decimal)4000).ToString("0.000"), new XFont("Verdana", 8, XFontStyle.BoldItalic), XBrushes.Gray, new XPoint(xTranslation * pasMesure_H + 20, maxGraph_Y + 10));
                                gfx.DrawString(((decimal)indexMeasure / (decimal)4000).ToString("0.000"), new XFont("Verdana", 8, XFontStyle.BoldItalic), XBrushes.Gray, new XPoint(xTranslation * pasMesure_H + 20, maxPage_Y - 15));
                            }

                            // affichage des points de dépassement de seuils
                            if (Ds_Datas[indexPivot] != null && Ds_Datas[indexPivot].Contains(Piv_Datas[indexPivot][indexMeasure].X))
                            {
                                gfx.DrawEllipse(XBrushes.Red, xTranslation * pasMesure_H + maxGraph_X - 0.5, -Piv_Datas[indexPivot][indexMeasure].Y * pasMesure_V + origineGraph - 0.5, 1, 1);
                                gfx.DrawString(Math.Round(Convert.ToDecimal(Piv_Datas[indexPivot][indexMeasure].Y), 3).ToString(), new XFont("Verdana", 3, XFontStyle.BoldItalic), XBrushes.Red, new XPoint(xTranslation * pasMesure_H + maxGraph_X + 7, -Piv_Datas[indexPivot][indexMeasure].Y * pasMesure_V + origineGraph));
                            }
                            xTranslation++;
                        }
                        catch(OutOfMemoryException oomEx)
                        {
                            MessageBox.Show(oomEx.Message, "Interruption de l'export", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                    if(!docCreated)
                        tempCount = countMax + 1 - Convert.ToInt32(pointsInRecouvrZone); //Affichage section suivante avec recouvrement précédant

                    ShowOnGraph(ref pen, ref gfx, origineGraph, maxValue, minValue, pasMesure_V, pasMesure_H, repereVerticaux_MeterToPt, pointsToSee);
                }
                globalTempCount = tempCount;
                gfx.Dispose();
                pdfPage.Close();

            }

            string path = SaveDocument_Pdf(pdfDoc);
            if (File.Exists(path))
                ShowDocument_Pdf(path);



Thanks in advance.
PS : i don t know if i m in the good sexion so sorry for my mistake in this case.


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 15, 2014 11:22 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
I don't have a solution. And without an SSCCE it will be difficult to find this bug.

Are you using WPF build or GDI+ build?
Is your program running in 64 bit mode? There shouldn't be memory problems with 64 bit mode, so try 64 bit mode if possible.

You can try adding the pages in chunks. Add 50 pages, save the PDF, close everything.
Open the PDF, append 50 pages, save, close. Open the PDF ...

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 06, 2014 12:13 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
This could be a problem of Large Object Heap (LOH) fragmentation.

With .NET 4.5.1 and later you can compact the LOH. Applications that create PDF files with a few hundred images should compact the LOH from time to time.

Code:
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;

GC.Collect();     // This will cause the LOH to be compacted (once).

See also:
http://blogs.msdn.com/b/mariohewardt/ar ... -heap.aspx

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 12, 2014 3:58 pm 
Offline

Joined: Thu Jul 03, 2014 9:19 pm
Posts: 2
You are allocating a lot of temporary objects like XFont, XBrush and XPen. Maybe you're better off allocating these outside the loop and the reusing them.

hth
gerd


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 46 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group