Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 65691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:59:10+00:00 2026-05-10T18:59:10+00:00

The following code snippet illustrates a memory leak when opening XPS files. If you

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:59:11+00:00Added an answer on May 10, 2026 at 6:59 pm

    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;

            Using XPSItem As New Windows.Xps.Packaging.XpsDocument(PathToTestXps, System.IO.FileAccess.Read)             Dim FixedDocSequence As Windows.Documents.FixedDocumentSequence             Dim DocPager As Windows.Documents.DocumentPaginator              FixedDocSequence = XPSItem.GetFixedDocumentSequence             DocPager = FixedDocSequence.DocumentPaginator             DocPager.ComputePageCount()              ' This is the fix, each page must be laid out otherwise resources are never released.'             For PageIndex As Integer = 0 To DocPager.PageCount - 1                 DirectCast(DocPager.GetPage(PageIndex).Visual, Windows.Documents.FixedPage).UpdateLayout()             Next             FixedDocSequence = Nothing         End Using 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 109k
  • Answers 109k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't think you should do anything to stop cheating… May 11, 2026 at 9:21 pm
  • Editorial Team
    Editorial Team added an answer Try this in addition to allowing access to the desktop:… May 11, 2026 at 9:21 pm
  • Editorial Team
    Editorial Team added an answer For the first question. $start = microtime(true); $end = microtime(true);… May 11, 2026 at 9:21 pm

Related Questions

I'm writing cross platform C++ code (Windows, Mac). Is there a way to check
It seems UITextFields convert all non-breaking spaces (nbsp, U+00A0) to spaces (U+0020). This causes
I'm in a curious situation where I previously had no problem achieving what I'm
Consider the following code snippet private void ProcessFile(string fullPath) { XmlTextReader rdr = new

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.