Saving a FixedDocument to an XPS file causes memory leak

后端 未结 2 1892
耶瑟儿~
耶瑟儿~ 2021-02-05 23:31

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

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-06 00:09

    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();
    }
    

提交回复
热议问题