Thursday, December 8, 2011

FlowDocument issues

We created this service that would fire prints on a network printer. When tested for performance, the code showed signs of memory leaks. This was strange, because I was expecting the .Net's garbage collector to cleanup unused objects. Perhaps there were some objects that were left to be disposed.

So, as part of our analysis, using WinDebugger, a memory dump of the process was collected. Using DebugDiag, this dump was analyzed. Multiple objects of type FlowDoument (used in CreateXpsDoc) were found blocking the finalizer queue. Googling for this type led us to some similar problems with this type being discussed in tech forums. It seems FlowDocument uses System.Windows.Threading.Dispatcher to free all resources

Reference: http://stackoverflow.com/questions/952985/flowdocument-memory-issue-in-c-sharp

Solution:
Execute these lines of code between printing and deleting the xps file.
var dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
dispatcher.BeginInvokeShutdown(System.Windows.Threading.DispatcherPriority.SystemIdle);
System.Windows.Threading.Dispatcher.Run();

No comments:

Post a Comment