可能原因FileStream.Write()抛出一个OutOfMemoryException? [英] Possible reasons for FileStream.Write() to throw an OutOfMemoryException?

查看:1021
本文介绍了可能原因FileStream.Write()抛出一个OutOfMemoryException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有10个线程编写成千上万的小缓冲区(16-30字节每个)在随机位置一个巨大的文件。一些线程抛出OutOfMemoryException异常对FileStream.Write()O $ P $而不能使。

I have 10 threads writing thousands of small buffers (16-30 bytes each) to a huge file in random positions. Some of the threads throw OutOfMemoryException on FileStream.Write() opreation.

是什么原因造成的OutOfMemoryException异常?寻找什么?

What is causing the OutOfMemoryException ? What to look for?

我使用的是像这样的FileStream(每一个书面的项目 - 这code来自10个不同的线程上运行):

I'm using the FileStream like this (for every written item - this code runs from 10 different threads):

using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite, BigBufferSizeInBytes, FileOptions.SequentialScan))
{
 ...
 fs.Write();
}

我怀疑是所有分配的的FileStream内部缓冲区不要被GC的发布时间。我不明白的是为什么CLR而不是抛出,不只是运行一个GC周期并释放所有未使用的缓冲区?

I suspect that all the buffers allocated inside the FileStream don't get released in time by the GC. What I don't understand is why the CLR, instead of throwing, doesn't just run a GC cycle and free up all the unused buffers?

推荐答案

如果10线程打开文件作为code显示,那么你有最多十个未予处置的FileStream对象在任何一个时间。是的文件流的确实有一个内部缓冲区,你在你的codeBigBufferSizeInBytes指定尺寸。可否请你透露确切的价值?如果这是足够大(例如〜100MB),那么它很可能是问题的根源。

If ten threads are opening files as your code shows, then you have a maximum of ten undisposed FileStream objects at any one time. Yes, FileStream does have an internal buffer, the size of which you specify with "BigBufferSizeInBytes" in your code. Could you please disclose the exact value? If this is big enough (e.g. ~100MB) then it could well be the source of the problem.

默认情况下(即当你没有指定一个数字在建),此缓冲区为4KB,这是通常可以适用于大多数应用。一般来说,如果你真的关心磁盘的写入性能,那么你可能会增加这一个一对夫妇100KB的,但并不多。

By default (i.e. when you don't specify a number upon construction), this buffer is 4kB and that is usually fine for most applications. In general, if you really care about disk write performance, then you might increase this one to a couple of 100kB but not more.

然而,对于特定应用这样就没有多大意义,因为表示缓冲区永远不会包含超过16-30个字节你写进去,你的Dispose(前)FileStream对象。

However, for your specific application doing so wouldn't make much sense, as said buffer will never contain more than the 16-30 bytes you write into it before you Dispose() the FileStream object.

要回答你的问题,抛出一个OutOfMemoryException异常,只有当请求的内存无法分配的 GC已运行。同样,如果缓冲区非常大,则系统可以有足够的内存离开了,​​只是没有一个连续块。这是因为大对象堆从不压实。

To answer your question, an OutOfMemoryException is thrown only when the requested memory can't be allocated after a GC has run. Again, if the buffer is really big then the system could have plenty of memory left, just not a contiguous chunk. This is because the large object heap is never compacted.

这篇关于可能原因FileStream.Write()抛出一个OutOfMemoryException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆