title: Saving a FixedDocument to an XPS file causes memory leak
body: <p>I have created a .NET Windows Service which performs certain actions and generates reports. These reports are XPS documents which I save in a certain directory.</p>
  
  <p>Being familiar with WPF, the way I have chosen to create the reports is to instantiate a    <code>System.Windows.Documents.FixedDocument</code>, adding <code>FixedPage</code> objects with content as required.</p>
  
  <p><strong>My problem is that the Service memory usage goes up and up and up over time as it runs.</strong></p>
  
  <p>At first, I went through my code rigorously, ensuring all disposable objects were disposed, etc, and other obvious memory leak candidates, but still had the problem. I then used the CLR Profiler to look at the memory usage of the Service in detail.</p>
  
  <p>I found that as the service generates these <code>FixedDocument</code> reports, and saves them as XPS files, all the various UI elements associated with <code>FixedDocument</code> objects (<code>Dispatcher</code>, <code>FixedPage</code>, <code>UIElementCollection</code>, <code>Visual</code>, etc) are staying in memory.</p>
  
  <p>This doesn't seem to happen when I do the same thing in my WPF apps, and so my hunch is that it has something to do with the WPF UI Dispatcher model being used outside of a WPF app.</p>
  
  <p>How can I "dispose" my <code>FixedDocument</code> objects when using them in a service like this (or outside a WPF app in general)?</p>
  
  <p>======== EDIT =========</p>
  
  <p>OK, I've found that my memory leak is not specifically to do with creating/populating a FixedDocument. If I do so, but don't actually ever save it to disk as a XPS, the memory leak doesn't happen. So, my problem must be to do with the save as XPS file.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>var paginator = myFixedDocument.DocumentPaginator;
  var xpsDocument = new XpsDocument(filePath, FileAccess.Write);
  var documentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument);                         
  documentWriter.Write(paginator);
  xpsDocument.Close();
  </code></pre>
  
  <p>What I've tried:</p>
  
  <ul>
  <li>Manual garbage collection</li>
  <li>Calling <code>UpdateLayout()</code> on each page of <code>myFixedDocument</code> before getting it's paginator (as suggested in the answer below) - I've also tried passing <code>myFixedDocument</code> directly into <code>Write()</code>  i.e. not the paginator</li>
  <li>Putting those lines of code in their own thread and manually shutting down Dispatchers</li>
  </ul>
  
  <p>Still no luck.</p>
  
  <p>========== WORKAROUND ==========</p>
  
  <p>By isolating the above code into its own AppDomain using the general method shown in the example at <a href="http://msdn.microsoft.com/en-us/library/system.appdomain.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.appdomain.aspx</a>, the memory leak no longer affects my service (I say "no longer affects" because it still happens, but when the AppDomain is unloaded, all leaked resources are unloaded with it).</p>
  
  <p>I would still be keen to see a real solution.</p>
  
  <p>(On a related note, for those interested, using a separate AppDomain caused a memory leak in the PDFSharp component I was using to turn certain XPS files into PDF files. Turns out PDFSharp uses a global font cache that in normal circumstances doesn't grow significantly. But the cache was growing and growing after using these AppDomains. I edited the PDFSharp source code to enable me to manually clear out the FontDescriptorStock and FontDataStock, solving the issue.)</p>
  
  <p>========== SOLUTION ==========</p>
  
  <p>See my answer below for final solution.</p>