6

I am trying to debug an OutOfMemoryException that occurs when creating a fairly large ZIP file using System.IO.Packaging.ZipPackage.

The code is iterating through a large list of objects, doing the following for each object.

  1. Serializing the object data to a temporary file.
  2. Creating a PackagePart for the file.
  3. Copy from a source System.IO.Stream to another:
    • Source stream: FileStream
    • Target stream: PackagePart::GetStream() => MS.Internal.IO.Zip.ZipIOModeEnforcingStream

Finally it calls Package::Close() which saves the file.

The problem I am having is that for a particularly large list of objects, I am seeing an OutOfMemoryException (the x86 process size is getting to about 1.2GB in size).

I was thinking about partitioning the object data into chunks so I only process a smaller amount per loop (i.e. steps 1-3 above). The idea is that I would create n ZIP files in a temporary directory, and then find a way to combine them into a single archive.

Is this possible using System.IO.Packaging? What would I use to combine the parts?

Or is there a better way to fix this?

LeopardSkinPillBoxHat
  • 28,915
  • 15
  • 75
  • 111

2 Answers2

3

Calling the Flush method on the Package object in between creating a new package should probably solve the problem as that would cause the memory buffer to be flushed to disk.

Ankur
  • 33,367
  • 2
  • 46
  • 72
  • Thanks - this worked for me. I ended up flushing it between every 500 files that were added to the ZIP file. This was a good tradeoff between memory and performance. – LeopardSkinPillBoxHat Oct 24 '11 at 04:32
0

I would use the DotNetZip library (http://dotnetzip.codeplex.com/). I've tried several zip libraries (System.IO like you are currently using and also SharpZibLib) and by far the easiest to use is the DotNetZip library.

You'll almost certainly end up with fewer lines of code and I found the memory usage to be very good (had a problem in a virtual machine environment which I reported and a new release fixed it).

Ben Robbins
  • 2,889
  • 2
  • 31
  • 32
  • My original question wasn't very clear about this, but this is actually a defect in existing code and I didn't want to have to re-write this code too much (I just want to fix the crash). I edited the question to make this more clear. – LeopardSkinPillBoxHat Oct 21 '11 at 03:56