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.
Being fa
I eventually found an answer, which is two parts.
Firstly, after saving my XPS document to disk and closing/disposing the XpsDocument
, I run the following line of code:
Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.SystemIdle, new DispatcherOperationCallback(delegate { return null; }), null);
This gets rid of all the Dispatcher
objects hanging around in memory.
While the above sorts out most of the memory issues, I noticed there were still FixedPage objects along with other UI objects still in memory. Manually clearing out my FixedDcoument seems to get rid of them:
foreach (var fixedPage in FixedDocument.Pages.Select(pageContent => pageContent.Child)) {
fixedPage.Children.Clear();
}