Saving a FixedDocument to an XPS file causes memory leak

后端 未结 2 1891
耶瑟儿~
耶瑟儿~ 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-05 23:59

    From this, it looks like you have to call .UpdateLayout() at least once to avoid memory leak

    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
提交回复
热议问题