When performing file IO in .NET, it seems that 95% of the examples that I see use a 4096 byte buffer. What’s so special about 4kb for a buffer length? Or is it just a convention like using i for the index in a for loop?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That is because 4K is the default cluster size for for disks upto 16TB. So when picking a buffer size it makes sense to allocate the buffer in multiples of the cluster size.
A cluster is the smallest unit of allocation for a file, so if a file contains only 1 byte it will consume 4K of physical disk space. And a file of 5K will result in a 8K allocation.
Update: Added a code sample for getting the cluster size of a drive