eli5: what does (de-)fragmentation even mean?

443 views

After reading another post about why defragmentation isn’t as necessary with modern devices, i started wondering what exactly fragmentation even is. How and why does it happen and doesn’t it screw up your data?

In: 0

11 Answers

Anonymous 0 Comments

So imagine a traditional hard drive – you have a spinning platter and a head that moves around on it.

Here’s a simplified example.

When you write a file called “myfile” to disk, you create an entry in a directory table saying “myfile starts on sector 17.” Now maybe your file is 6 sectors long, but sector 21 is used by something else. So you have sectors 17,18,19, and in sector 20 you say “the rest of this file starts at sector 44.”

So your drive head moves over to sector 44, and reads sector 44, 45, and 46 to get the complete file.

Your file is in two fragments.

Now if you have a lot of big files that are constantly being written and re-written to disk, and especially if the disk is mostly full, the odds are very good that your file will get split up into a LOT of fragments, and every time you have to jump to a new fragment, you are probably going to have to move the head somewhere and then wait for the right portion of the disk to spin under it. This can definitely increase the time to read a file.

Defragmenting does its best to undo this process – it moves data blocks around on the disk to consolidate some free space, and then moves a fragmented file into that free space. Then repeats this many many times.

So, **why isn’t it as important any more?**

Firstly because we have solid-state drives (SSD), which don’t have physical moving parts to shuffle around. Reading data blocks is roughly a constant rate, no matter where they reside on the “disk,” and how fragmented they are. They’re also ridiculously fast compared to spinning drives.

Secondly, because modern filesystems are much better at addressing the problem of fragmentation. For example, they don’t start writing a file at the first spot available on a disk, or even at the first spot big enough for the entire file. Instead, they look for the smallest space that will hold the entire file; and in some cases, will deliberately fragment a file to avoid leaving tiny gaps of free space scattered across the disk. They also may do things like calculating the shortest ‘travel time’ from the end of one fragment to the start of the next.

Finally, spinning disks now have a big chunk of cache on them which will pre-load more data; and operating systems cache read data in memory as well. Less and less of the time we spend waiting for our computer is tied to the hard drive.

You are viewing 1 out of 11 answers, click here to view all answers.