none
Does PackagePart stream have a caching feature? RRS feed

  • Question

  • Note: This has already been discussed in "http://stackoverflow.com/questions/3819564/reading-from-packagepart-stream-does-not-release-memory" which resulted in the conclusion that maybe the Package has an internal caching feature. Can this be confirmed? Or is the cause something else?

     

    In our application, we are reading an XPS file using the System.IO.Packaging.Package class. When we read from a stream of a PackagePart, we can see from the Task Manager that the application's memory consumption rises. However, when the reading is done, the memory consumption doesn't fall back to what it was before reading from the stream.

    To illustrate the problem, I wrote a simple code sample that you can use in a stand alone 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();  
      }
     }
    The ReadPackage() method will read all the PackagePart objects' stream contents into a local array. In the sample, I used a 1000 page XPS document as the package source in order to easily see the memory consumption change of the application. On my machine, the stand alone app's memory consumption starts at 18MB then rises to 100MB after calling the method. Calling the method again can raise the memory consumption again but it can fall back to 100MB. However, it doesn't fall back to 18MB anymore.

    Does the Package have an internal caching feature?

    Thank you!


    Wednesday, October 6, 2010 6:32 AM