I'm reading the book "O'Reilly Java IO" and there is a recommendation:
Files аrе often best written in small multiples of the block size of the disk, typically 512, 1024, or 2048 bytes.
I tried to find any explanation, but I couldn't. I have just some ideas. But it would be great if someone explained or shared the link, why it is a good practice. Thanks.
To prevent uneccesary Read-write-modify cycles on the harddisk.
ideally you "output the data" as close as possible to the actual sector size.
Everytime data is being written the entire sector is Read, then modified with your data, then written back to disk.
If you flush out 5 bytes at a time, thats a lot of IO operations that have to happen. Lets say you have a harddrive with sector size 4096, that's roughly a 120 read - modify -write operations to complete.
Whilst if you buffer it to the the max, you only have one read write modify operation to complete, which will cause less waiting for harddrive to complete this task.
A lot of operating system processes and probaly harddrive firmware exists to wait a bit to see if more data will be added to sector, but it's best to help to make sure the harddrive write cache can be flushed to disk sooner.
Interesting reading:
What goes on behind the curtains during disk I/O?
http://www.seagate.com/tech-insights/advanced-format-4k-sector-hard-drives-master-ti/
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