Why is it better to write files into small pieces when recording?

I read the book "O'Reilly Java IO", and there is a recommendation:

Files are often best written in small multiples of the disk block size, usually 512, 1024 or 2048 bytes.

I tried to find some explanation, but could not. I have only some ideas. But it would be great if someone explained or shared a link why this is good practice. Thanks.

+5
source share
1 answer

To avoid unnecessary read-write-change cycles on the hard drive.

Ideally, you β€œoutput the data” as close as possible to the actual sector size.

Each time the data is written, the entire sector is read, then modified by your data, and then written back to disk.

If you wash 5 bytes at a time, this is a lot of I / O operations that must happen. Suppose you have a hard disk with a sector size of 4096, which is about 120 read-modify -write operations to complete.

While if you buffer it to the maximum, you only have to perform one read operation on the write, which will cause the harddrive to stop waiting for this task to complete.

Many operating system processes and hard drive firmware exist to wait a bit to see if more data will be added to the sector, but it is best to ensure that the write cache of the hard drive can be flushed to the disk earlier.

Interesting reading:

What happens behind the curtains during disk I / O?
http://www.seagate.com/tech-insights/advanced-format-4k-sector-hard-drives-master-ti/

+2
source

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


All Articles