The following code snippet illustrates a memory leak when opening XPS files. If you run it and watch the task manager, it will grow and not release memory until the app exits.
'****** Console application BEGINS.
Module Main
Const DefaultTestFilePath As String = "D:\Test.xps"
Const DefaultLoopRuns As Integer = 1000
Public Sub Main(ByVal Args As String())
Dim PathToTestXps As String = DefaultTestFilePath
Dim NumberOfLoops As Integer = DefaultLoopRuns
If (Args.Count >= 1) Then PathToTestXps = Args(0)
If (Args.Count >= 2) Then NumberOfLoops = CInt(Args(1))
Console.Clear()
Console.WriteLine("Start - {0}", GC.GetTotalMemory(True))
For LoopCount As Integer = 1 To NumberOfLoops
Console.CursorLeft = 0
Console.Write("Loop {0:d5}", LoopCount)
' The more complex the XPS document and the more loops, the more memory is lost.
Using XPSItem As New Windows.Xps.Packaging.XpsDocument(PathToTestXps, System.IO.FileAccess.Read)
Dim FixedDocSequence As Windows.Documents.FixedDocumentSequence
' This line leaks a chunk of memory each time, when commented out it does not.
FixedDocSequence = XPSItem.GetFixedDocumentSequence
End Using
Next
Console.WriteLine()
GC.Collect() ' This line has no effect, I think the memory that has leaked is unmanaged (C++ XPS internals).
Console.WriteLine("Complete - {0}", GC.GetTotalMemory(True))
Console.WriteLine("Loop complete but memory not released, will release when app exits (press a key to exit).")
Console.ReadKey()
End Sub
End Module
'****** Console application ENDS.
The reason it loops a thousand times is because my code processes lots of files and leaks memory quickly forcing an OutOfMemoryException. Forcing Garbage Collection does not work (I suspect it is an unmanaged chunk of memory in the XPS internals).
The code was originally in another thread and class but has been simplified to this.
Any help greatly appreciated.
Ryan
Well, I found it. It IS a bug in the framework and to work around it you add a call to UpdateLayout. Using statement can be changed to the following to provide a fix;
I can't give you any authoritative advice, but I did have a few thoughts:
Add UpdateLayout cannot solve the issue. According to http://support.microsoft.com/kb/942443, "preload the PresentationCore.dll file or the PresentationFramework.dll file in the primary application domain" is needed.
Interesting. The problem is still present in .net framework 4.0. My code was leaking ferociously.
The proposed fix -- where UpdateLayout is called in a loop immediately after creation of the FixedDocumentSequence did NOT fix the problem for me on a 400 page test document.
However, the following solution DID fix the problem for me. As in previous fixes, I moved the call to GetFixedDocumentSequence() outside the for-each-page loop. The "using" clause... fair warning that I'm still not sure it's correct. But it's not hurting. The document is subsequently re-used to generate page previews on-screen. So it doesn't seem to hurt.
In reality, the fix line goes inside my GetXpsPageAsBitmap routine (ommited for clarity), which is pretty much identical to previously posted code.
Thanks to all who contributed.
Ran into this today. Interestingly, when I gazed into things using Reflector.NET, I found the fix involved calling UpdateLayout() on the ContextLayoutManager associated with the current Dispatcher. (read: no need to iterate over pages).
Basically, the code to be called (use reflection here) is:
Definitely feels like a small oversight by MS.
For the lazy or unfamiliar, this code works:
FxCop will complain, but maybe it's fixed in the next framework version. The code posted by the author seems to be "safer" if you would prefer not to use reflection.
HTH!