Possible reasons for FileStream.Write () to throw an OutOfMemoryException?

I have 10 streams that write thousands of small buffers (16-30 bytes each) to a huge file in random positions. Some of the threads raise an OutOfMemoryException in FileStream.Write () opreation.

What causes an OutOfMemoryException? What to look for?

I use FileStream like this (for each element written - this code works from 10 different threads):

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

I suspect that all buffers allocated inside FileStream will not be released by the GC in a timely manner. I don’t understand why, instead of throwing the CLR, it doesn’t just start the GC loop and free up all unused buffers?

+3
source share
5 answers

If ten threads open files, as your code shows, then you have at most ten undisclosed FileStream objects at any given time. Yes, FileStream has an internal buffer, the size of which you specify using "BigBufferSizeInBytes" in your code. Could you tell us the exact meaning? If it is large enough (for example, 100 MB), then this may be the source of the problem.

By default (i.e. when you do not specify a number when building), this buffer is 4kB, and this is usually great for most applications. In general, if you really care about the performance of writing to disk, then you can increase this figure to a few 100 kB, but no more.

, 16-30 , , Dispose() FileStream.

, OutOfMemoryException , , GC . , , , . , .

+2

, , , , .

, , .

, . , , (Spooky Dooky).

, , (-, ), , , . , ( -, )....

OOP! "BigBufferSizeInBytes", ...

, ( - ), "mbuf", , ... (.. insadential ).

+1

FileStream. , , " " - ? , (.. /).

- ? FileStream ... , , .

0

, .NET Framework .

, , , FileStream . 'using' , fs.Write(). GC-.

FileStream /. , .

. , .NET 4.0. .

Dave

0

, - ?

, . , , - , .

FileStream.Write() - , , , , . BigBufferSizeInBytes (4k), , ...

, , , !

0

Source: https://habr.com/ru/post/1703270/


All Articles