Reading from a PackagePart stream does not free memory

In our application, we read the XPS file using the System.IO.Packaging.Package class. When we read from the PackagePart stream, the task manager shows that the memory consumption of the application is increasing. However, when reading is done, memory consumption does not return to what it was before reading from the stream.

To illustrate the problem, I wrote a simple code example that you can use in a standalone wpf application.

 public partial class Window1 : Window
 {
        public Window1()
        {
            InitializeComponent();

            _package = Package.Open(@"c:\test\1000pages.xps", FileMode.Open, FileAccess.ReadWrite, FileShare.None);

        }

        private void ReadPackage()
        {
            foreach (PackagePart part in _package.GetParts())
            {
                using (Stream partStream = part.GetStream())
                {
                    byte[] arr = new byte[partStream.Length];
                    partStream.Read(arr, 0, (int)partStream.Length);
                    partStream.Close();
                }
            }
        }

        Package _package;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ReadPackage();      
        }
 }

ReadPackage() PackPart . 1000- XPS , . 18 , 100 . , 100 . 18 .

- PackagePart? ? , PackagePart , .

!

+5
1

, " " , , , ? , , . , .

, , Microsoft CLR.

, , .NET, . (LOH), .NET - , . , LOH , LOH, , .

- PackagePart? ?

, , . , :

using (var package = Package.Open(@"c:\test\1000pages.xps", FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
  // ... process the package
}

using , , , .

_package , - Close() ( IDisposable.Dispose()), . GC.Collect() , . (, ), _package, , , .

0

All Articles