2

When I use c# to read a .xps file, I get an OutOfMemoryException.
I got some information from Opening XPS document in .Net causes a memory leak, but it seems not work in my code.

My Code below:

using (XpsDocument document = new XpsDocument(xpsPath, System.IO.FileAccess.Read))
{
    FixedDocumentSequence fds = document.GetFixedDocumentSequence();

    int pageCount = fds.DocumentPaginator.PageCount;

    for (int i = 0; i < pageCount; i++)
    {
        DocumentPage source = fds.DocumentPaginator.GetPage(i);
        Visual v = source.Visual;
        FixedPage fixedPage = (FixedPage)v;

        fixedPage.UpdateLayout();
    }           
    document.Close();
}
  1. When I remove the line fixedPage.UpdateLayout(). Through Win8's "Task Manager", the current program's use of memory increased 10MB each time while I read one .xps file.
  2. When I add this line, the use of memory still increased about 1MB each time.

Any one can help me?

Community
  • 1
  • 1
Gary Chen
  • 105
  • 6
  • Your code does not contain `fixedPage.Update()`, did you mean `fixedPage.UpdateLayout()` or is the line missing from your code? – Manfred Radlwimmer May 09 '16 at 09:07
  • So sorry about that, It's fixedPage.UpdateLayout() and I updated my question. – Gary Chen May 09 '16 at 16:59
  • Does `DocumentPage` or `Visual` implement `IDisposable`? It might be that these allocate native resources (my suspicion is raised by `GetPage(int)` being a method, not a property), and you're leaking resources as you iterate. – Martin Costello May 09 '16 at 17:04
  • I add `source.Dispose();` to my code blow `fixedPage.UpdateLayout();`. Still not work. – Gary Chen May 09 '16 at 17:36

0 Answers0