5

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.

LEQADA
  • 1,913
  • 3
  • 22
  • 41
Illia
  • 158
  • 1
  • 10
  • General note in case you are thinking about implementing something that acknowledges this statement: unless you are doing some VERY high performance IO thingy (writing 100s of MB per second) it is not worth worrying about this (even then it might not make sense). But still would nice that you want to know *why*. – luk2302 Aug 11 '17 at 13:27
  • 1
    The accepted answer on the following question should give you some hints, even though the question was about the reading part : https://stackoverflow.com/questions/236861/how-do-you-determine-the-ideal-buffer-size-when-using-fileinputstream – Arnaud Aug 11 '17 at 13:32
  • Welcome to Stack Overflow! Please take the [tour](http://stackoverflow.com/tour), have a look around, and read through the [help center](http://stackoverflow.com/help), in particular [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) and [What topics can I ask about here?](http://stackoverflow.com/help/on-topic). – Timothy Truckle Aug 11 '17 at 13:35

1 Answers1

2

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/

Tschallacka
  • 27,901
  • 14
  • 88
  • 133