使用 BZip2 (SharpZipLib) 时出现 OutOfMemoryException

发布于 2024-09-11 23:22:56 字数 2158 浏览 0 评论 0原文

我使用Asp.net、.net 3.5、win2003、iis 6.0。

我使用 Oracle 收集文件,将文件以 SharpZipLib.BZip2 压缩格式保存在 Oracle 表的 RAW 字段中。

我的应用程序是 Web,我使用 WCF 服务来获取文件的数据(字节数组)。 aspx 页面将文件发送给用户(下载文件)。

我的问题:

我从 Oracle 读取数据(我调用 WCF 服务)。我得到字节数组(byte[]),

我尝试使用 SharpZipLib.BZip2 解压缩文件

using (MemoryStream inData = new MemoryStream(data))
{ 
using (MemoryStream outData = new MemoryStream())
{
          BZip2.Decompress(inData, outData); //<==================== Fails here OutOfMemoryException
          return outData.ToArray();
}

}

,错误是因为“未压缩”文件很大,非常大(> 500 MB)!

压缩文件:4MB

未压缩文件:> 500 MB

我做这样的测试:

BufferedStream bufin = new BufferedStream(instream);

            using (MemoryStream outData = new MemoryStream())
            {
                BZip2.Decompress(bufin, outData);

                return outData.ToArray();
            }

但我得到相同的 OutOfMemoryException

跟踪堆栈。解压缩

   en System.IO.MemoryStream.set_Capacity(Int32 value)
   en System.IO.MemoryStream.EnsureCapacity(Int32 value)
   en System.IO.MemoryStream.WriteByte(Byte value)
   en Reale.Frk.Compression.BZip2.BZip2.Decompress(Stream inStream, Stream outStream)

异常代码的 SharpZipLib.BZip2

public static void Decompress(Stream inStream, Stream outStream) 

            {

                  if ( inStream == null ) {

                        throw new ArgumentNullException("inStream");

                  }

                  if ( outStream == null ) {

                        throw new ArgumentNullException("outStream");

                  }


                  using ( outStream ) {

                        using ( BZip2InputStream bzis = new BZip2InputStream(inStream) ) {

                             int ch = bzis.ReadByte();

                             while (ch != -1) {

                                   outStream.WriteByte((byte)ch);

                                   ch = bzis.ReadByte();

                             }

                        }

                  }

            }

任何建议,评论,示例源代码?

I use Asp.net , .net 3.5, win2003, iis 6.0.

I use Oracle for gathering files, saving file in SharpZipLib.BZip2 compressed format in field RAW in table Oracle.

My application is Web, and I use WCF Service for get data (array of bytes) of a file. The aspx page send file to user (download file).

My issue-problem:

I read DATA from Oracle, (I call to WCF Service). I get array of bytes (byte[]),

I try Uncompress file using SharpZipLib.BZip2

using (MemoryStream inData = new MemoryStream(data))
{ 
using (MemoryStream outData = new MemoryStream())
{
          BZip2.Decompress(inData, outData); //<==================== Fails here OutOfMemoryException
          return outData.ToArray();
}

}

the error is because the file "uncompressed" is big, very big (> 500 MB) !!!

compressed file: 4MB

uncompressed file: > 500 MB

I do test like this:

BufferedStream bufin = new BufferedStream(instream);

            using (MemoryStream outData = new MemoryStream())
            {
                BZip2.Decompress(bufin, outData);

                return outData.ToArray();
            }

But I get the same OutOfMemoryException

Trace Stack of Exception

   en System.IO.MemoryStream.set_Capacity(Int32 value)
   en System.IO.MemoryStream.EnsureCapacity(Int32 value)
   en System.IO.MemoryStream.WriteByte(Byte value)
   en Reale.Frk.Compression.BZip2.BZip2.Decompress(Stream inStream, Stream outStream)

Code of SharpZipLib.BZip2.Decompress

public static void Decompress(Stream inStream, Stream outStream) 

            {

                  if ( inStream == null ) {

                        throw new ArgumentNullException("inStream");

                  }

                  if ( outStream == null ) {

                        throw new ArgumentNullException("outStream");

                  }


                  using ( outStream ) {

                        using ( BZip2InputStream bzis = new BZip2InputStream(inStream) ) {

                             int ch = bzis.ReadByte();

                             while (ch != -1) {

                                   outStream.WriteByte((byte)ch);

                                   ch = bzis.ReadByte();

                             }

                        }

                  }

            }

any suggestions, comments, sample source code ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我闹别瞎闹 2024-09-18 23:22:56

跳过MemoryStream并直接写入文件。

否则向服务器添加更多内存。

指定 MemoryStream 初始容量的另一个选项。

Skip the MemoryStream and write directly to a file.

Else add more memory to the server.

Another option to specify the initial capacity for the MemoryStream.

木格 2024-09-18 23:22:56

您可能会遇到内存不足错误,因为您的系统上没有 500 MB 内存流的单个连续内存区域,但您可能有足够的非连续内存块。使用 MemoryTributary 类代替,它可能会起作用。该类可能需要一些调整(如果我没记错的话,它可能不会干净地返回最后一个块并用 ASCII(0) 填充它)

Chances are that you are getting out of memory error because the single contiguous memory area for the memory stream of 500 MB is not available on your system, but you may have enough non-contiguous memory blocks. Use MemoryTributary class instead and it might work. The class may require some tweaking (if I remember correctly it may not cleanly return the very last block and pad it with ASCII(0))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文