从 ResponseStream 内存不足异常创建内存流

发布于 2024-07-17 03:03:46 字数 741 浏览 2 评论 0原文

我正在调用 httprequest,它在响应流中返回 pdf 文件。 这对于较小的 pdf 来说效果很好,但文件大小不超过 25-30MB,它会返回内存不足异常。

        MemoryStream memStream = new MemoryStream();
        byte[] buffer = new byte[2048];

        int bytesRead = 0;
        do
        {
            bytesRead = _ResponseStream.Read(buffer, 0, buffer.Length);
            memStream.Write(buffer, 0, bytesRead);
        } while (bytesRead != 0);

        _ResponseStream.Close();
        _ResponseStream.Dispose();
        _ResponseStream = null;

        if (memStream.Length > 0)
            return memStream;
        else return null;

机器规格为四核 3.0GZ、4GB RAM(物理盒)。 PDF 的大小可能达到 60MB。 多个用户可以同时下载。 这会产生怎样的影响呢? 将响应流直接传送到 FileStream 会更好吗? 有这样做的最佳实践方法吗?

I am making a call to a httprequest which returns a pdf file in the responsestream. This works well for smaller pdf's, but not the file is up around 25-30MB, it is returning an out of memory exception.

        MemoryStream memStream = new MemoryStream();
        byte[] buffer = new byte[2048];

        int bytesRead = 0;
        do
        {
            bytesRead = _ResponseStream.Read(buffer, 0, buffer.Length);
            memStream.Write(buffer, 0, bytesRead);
        } while (bytesRead != 0);

        _ResponseStream.Close();
        _ResponseStream.Dispose();
        _ResponseStream = null;

        if (memStream.Length > 0)
            return memStream;
        else return null;

Machine specs are Quad Core 3.0GZ, 4GB RAM (Physical Box). It is possible that the PDF could reach sizes of 60MB. Multiple users can download at thte same time. How would this effect it? Would it be better to take the response stream straight to a FileStream? Is there a best practice way of doing this?

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

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

发布评论

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

评论(2

任谁 2024-07-24 03:03:46

这应该没问题 - 对于 MemoryStream 来说 60MB 相当小。 每次需要时容量都会加倍,因此您可能有大约 120MB 的后备阵列,但这不会破坏一台像样的机器。

你确定输入数据不大于这个吗? 你有多少内存? 你能先查出响应流的大小吗? 如果您可以创建具有正确容量的 MemoryStream 来开始,那将会有所帮助。

请注意,using 语句更适合处理响应流 - 调用 Close 和 Dispose,然后将变量设置为 null 是多余的。

That should be fine - 60MB is reasonably small for a MemoryStream. The capacity doubles each time it needs to, so you may have about 120MB of backing array, but again that shouldn't break a decent machine.

Are you sure the input data is no bigger than that? How much memory do you have? Can you find out the size of the response stream first? If you can create the MemoryStream with the right capacity to start with, that would help.

Note that a using statement is better for the handling of the response stream - calling Close and Dispose and then setting a variable to null is overkill.

╰沐子 2024-07-24 03:03:46

看来内存流确实在某个时刻内存不足了。 随着文件最终存储在文件系统上,我现在从 httpwebrequest 中获取原始流,并将其直接保存到文件中。

It seems that the memory stream was indeed running out of memory at a certain point. As the file eventually gets stored on the file system, I am now taking the original stream from the httpwebrequest, and saving it straight to a file.

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