I have this simple code which combines text files into one text file :
void Main()
{
const int chunkSize = 2 * 1024; // 2KB
var inputFiles = new[] { @"c:\1.txt", @"c:\2.txt", @"c:\3.txt" };
using (var output = File.Create(@"c:\output.dat"))
{
foreach (var file in inputFiles)
{
using (var input = File.OpenRead(file))
{
var buffer = new byte[chunkSize];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
}
}
}
My question is about the chunkSize
size.
How can I know if the number I've chosen is the right one ? (1024*2)
I'm trying to find the idle formula :
Assuming each file size is F mb
, and I have R mb
of Ram and the block size of my Hd is B kb
- is there any formula which I can build to find the idle buffer size ?
To check the buffer window, multiply the bit rate (bits per second) by the buffer window (in seconds) and divide by 1000 to get the size, in bits, of the buffer for the stream.
The disk buffer is usually quite small, ranging between 8 and 256 MiB, and the page cache is generally all unused main memory.
1024 is the exact amount of bytes in a kilobyte. All that line means is that they are creating a buffer of 16 KB. That's really all there is to it. If you want to go down the route of why there are 1024 bytes in a kilobyte and why it's a good idea to use that in programming, this would be a good place to start.
The default buffer size is 4096. 0 or 1 means that buffering should be disabled. Negative values are not allowed. public: property int BufferSize { int get(); void set(int value); }; C# Copy.
4KB is a good choice. for more info look to this:
File I/O with streams - best memory buffer size
Greetings
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With