PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

How do I release memory after creating a document?
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1694
Page 1 of 1

Author:  tynman [ Fri Jun 17, 2011 11:58 pm ]
Post subject:  How do I release memory after creating a document?

I'm creating a series of barcode catalogs, and am running into an Out of Memory exception when I run my program. I've tried removing the images from the catalog and the memory footprint is WAY smaller, but it still doesn't release any of the memory from the created PDFs until after they're all created. Here's the code I've come up with so far:

Code:
// individualCatalogs is a LiNQ expression pulling what I need from the database.
foreach (var catalog in individualCatalogs.ToList()) {
   using (var pdf = new InventoryOperations().GenerateBarcodeCatalog(catalog)) {
      using (FileStream s = File.Open(String.Format("MyCatalog{1}.pdf", catalog.Name), FileMode.Create)) {
         pdf.Save(s);
         pdf.Close();
         pdf.Dispose();
         s.Close();
         s.Dispose();
      }
   }
   GC.Collect();
}



As you can see, I've tried every trick I can think of to get the PDF to get collected after it's saved to disk. What have you guys tried? I need the barcodes in there too (XImage elements), and that's when things start crashing.

Author:  balaji [ Fri Jun 24, 2011 10:53 am ]
Post subject:  Re: How do I release memory after creating a document?

Hi,
Add these lines too

GC.GetTotalMemory(false);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.GetTotalMemory(true);

Regards,

Balaji.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/