将大文件 (300MB) 写入内存流时出现内存不足异常

发布于 2024-12-01 09:04:56 字数 70 浏览 1 评论 0 原文

使用大文件的 Http.Post 时出现内存不足异常。当我尝试将文件流数据写入内存流时,出现此异常。

I'm getting an Out of Memory Exception when using Http.Post of a large file. I'm getting this exception when I tried to write the filestream data to the memory stream.

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

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

发布评论

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

评论(3

浅笑轻吟梦一曲 2024-12-08 09:04:56

显然,将 300mb 数据放入内存会导致 CLR 达到其最大内存占用量。您是否考虑过将文件写入其他类型的流以避免同时将其全部存储在内存中?也许您可以将文件直接写入磁盘,然后分块处理该文件,而不是同时将整个文件加载到内存中。

Obviously putting 300mb of data into memory is causing the CLR to reach its maximum memory footprint. Have you considered writing the file to some other kind of stream to avoid having it all in memory at the same time? Perhaps you could write the file directly to disk, and then process the file in chunks afterward, rather than loading the whole thing into memory at the same time.

苍景流年 2024-12-08 09:04:56

以上答案都不适合我,这个答案可以完成这项工作:

http://blogs.msdn.com/b/johan/archive/2006/11/15/are-you-getting-outofmemoryexceptions-when-uploading-large-files.aspx

这些台词是关键:

    HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(yourUri);
    wr.KeepAlive = false;
    wr.Timeout = System.Threading.Timeout.Infinite;
    wr.ProtocolVersion = HttpVersion.Version10;

这里:

wr.AllowWriteStreamBuffering = false;

希望这可以帮助那里的人。

None of the above answers work for me, this one does the job:

http://blogs.msdn.com/b/johan/archive/2006/11/15/are-you-getting-outofmemoryexceptions-when-uploading-large-files.aspx

These lines are the key:

    HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(yourUri);
    wr.KeepAlive = false;
    wr.Timeout = System.Threading.Timeout.Infinite;
    wr.ProtocolVersion = HttpVersion.Version10;

And here:

wr.AllowWriteStreamBuffering = false;

Hope this can help someone out there.

做个少女永远怀春 2024-12-08 09:04:56

设置 HttpWebRequest.AllowWriteStreamBuffering=false。这将导致 HWR 立即发送数据,而不是将其缓冲在内存中。

Set HttpWebRequest.AllowWriteStreamBuffering=false. This will cause HWR to send the data immediately, instead of buffering it in memory.

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