We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

PDF Memory leak?

Hi there!

Just began on syncfusion PDF and wanted to ask a quick question :

I'm trying to generate a large amount of pages into a PDF. Here's my code :

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
        
            var watch = System.Diagnostics.Stopwatch.StartNew();
            PdfDocument doc = new PdfDocument();
            doc.EnableMemoryOptimization = true;

            for (int i = 0; i < 5000; i++){
                await GeneratePDF(doc, i);
                Debug.WriteLine("doc " + i.ToString());
            }

            using (MemoryStream stream = new MemoryStream()){
                await doc.SaveAsync(stream);
                doc.Close(true);

                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Debug.WriteLine("generation in " + elapsedMs.ToString() + "ms");


                StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
                StorageFile stFile = await local.CreateFileAsync("test.pdf", CreationCollisionOption.ReplaceExisting);
                using (Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite)){
                    using (Stream st = fileStream.AsStreamForWrite()){
                        st.SetLength(0);
                        st.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length);
                        st.Flush();
                    }
                }
                stream.Flush();
            }
        }

        public async Task GeneratePDF(PdfDocument doc,int index)
        {
            PdfPage page = doc.Pages.Add();
            PdfGraphics g = page.Graphics;
            PdfBrush brush = new PdfSolidBrush(System.Drawing.Color.FromArgb(255, 0, 0, 0));
            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 36);
            for(int i = 0;i < 1000; i++){
                g.DrawString("Hello world!", font, brush, new PointF(20, 20+i));
            }
        }

And here's what i'm seeing with the memory :




How can I free up the RAM after the generation?

2 Replies

RD Romane Donnet April 28, 2016 07:24 AM UTC

Apparently the image doesn't load in the forum

Memory Leak image


CM Chinnu Muniyappan Syncfusion Team April 29, 2016 01:12 PM UTC

Hi Donnet, 

Thank you for contacting Syncfusion support. 

We have created a simple sample without using any of the syncfusion assemblies, and here also we getting the same memory behavior , so we suspect this the actual behavior of the visual studio diagnosing tool, please refer the below sample for more details. 

And also we have tried your sample to ANTS Memory Profiler, we are not getting any memory leak, please refer the below screenshot. 
 

Please let us know if you have any concern. 

Regards, 
Chinnu 


Loader.
Live Chat Icon For mobile
Up arrow icon