Why do disks fragment?

18

3

As a part of regular Windows maintenance, I defragment my hard drive. But why does the hard drive fragment on NTFS and FAT* systems? Apparantly ext* does not, why is this? Should I also be defragmenting my USB drives?

Simon Sheehan

Posted 2011-06-04T00:55:29.140

Reputation: 8 641

Don't de-fragment solid state devices. Not even on Microsoft's Windows. It is bad for the solid state device and is not needed. – ctrl-alt-delor – 2017-03-31T17:33:54.797

Answers

22

Fragmentation is not the issue it was 30 years ago. Back then you had hard drives that were scarcely faster than floppies, and processor memory sizes that were minuscule. Now you have very fast drives and large processor memories, and sometimes substantial buffering on the hard drive or in the controller. Plus sector sizes have gotten larger (or files are allocated in larger blocks) so that more data is inherently contiguous.

Operating systems have gotten smarter as well. Whereas DOS 1.x would have fetched each sector from disk as it was referenced, a modern OS is able to see that you have a file open for sequential access and can reasonably predict that you'll be fetching additional sectors once you've consumed those you have now. Thus it can "pre-fetch" the next several (dozen) sectors.

And any more it's often better to not have a file contiguous. On a (large) system where the file system is spread across multiple drives a file can actually be accessed faster if it is "spread" as well, since multiple disks can be seeking the file simultaneously.

I defragment every 2-3 years, whether my box needs it or not.

[I'll add that the important thing is not so much whether the data on the disk gets defragmented as whether the free space does. FAT was terrible at this -- unless you defragged things kept getting worse and worse until there were no two contiguous blocks of free space. Most other schemes can coalesce free space and allocate pieces in a somewhat "smart" fashion so the fragmentation reaches a certain threshold and then stabilizes, rather than getting worse and worse.]

Daniel R Hicks

Posted 2011-06-04T00:55:29.140

Reputation: 5 783

I have not defragmented my system ever. It is now about 10 years old. I just did a scan for the first time, fragmentation levels are very low ( 2.3% non-contiguous). I am using ext4 file-system. Also I have never run a virus scan; Never a problem. – ctrl-alt-delor – 2017-03-31T17:40:02.327

2I have not defragmented a system since 2005. There, I said it. – BlueRaja - Danny Pflughoeft – 2011-06-04T03:25:47.047

2@BlueRaja, If you do a lot of disk operations on a rotational storage, defragmenting helps. Recently I found that running a build of my application (2GB of source code and assets) is about 20% faster after defragmentation, and the box was ~1 month old. – liori – 2011-06-04T09:43:43.563

2An informative and useful post, but not an answer to the original question. I'm surprised to see this accepted. – EMP – 2011-06-10T01:22:23.060

13

ALL file systems fragment. ext and other Linux file systems fragment less due to the way they're designed - to quote Wikipedia regarding the Linux Network Administrators' Guide:

Modern Linux filesystem(s) keep fragmentation at a minimum by keeping all blocks in a file close together, even if they can't be stored in consecutive sectors. Some filesystems, like ext3, effectively allocate the free block that is nearest to other blocks in a file. Therefore it is not necessary to worry about fragmentation in a Linux system.

I'd note though that ext4 has online defragmentation so eventually fragmentation IS an issue, even with Linux file systems.

Windows file systems have their clusters placed wherever there's space to put them, and defrag runs around and replaces them. With Linux, files are preferentially placed where there's enough space.

I'd note though, Windows 7 has scheduled defragmentation runs, so it isn't really necessary to run defrag manually.

Journeyman Geek

Posted 2011-06-04T00:55:29.140

Reputation: 119 122

That does not seem correct. When I studied filesystems at uni. We learnt that trying to minimise fragmentation was a mistake as it will lead to grater fragmentation. Therefore Unix filesystems try to optimise fragmentation, by placing big chunks next to big chunks and small chunks next to small chunks. This results in the disk getting to a small amount of fragmentation very quickly, but then stabilising. – ctrl-alt-delor – 2017-03-31T17:22:13.470

Ext4 will fragment for some uncommon situations: using a filesystem to only store log files. In this case files are only being slowly appended to. One mitigation is to write them uncompressed, and as part of the log rotation compress the files. This then de-fragments each file. Most other situation, you don't need to worry (just ignore it, it will be fine). – ctrl-alt-delor – 2017-03-31T17:26:50.530

I do note that scheduled defragmentation was not enabled by default on any of my computers. – Simon Sheehan – 2011-06-04T01:04:27.900

5ext* does not defragment as it goes along. https://trac.transmissionbt.com/ticket/849 and http://www.h-online.com/open/features/Tuning-the-Linux-file-system-Ext3-746480.html. ext* minimises fragmentation by storing contiguous data at random locations on the disk. Random offsets spread the contiguous files out around the disk and reduce the need for fragmenting a file. – ta.speot.is – 2011-06-04T01:04:56.473

Any word on how Reiser/XFS avoid fragmentation? I've always wondered about them, but they seem largely undocumented in relation to ext. – new123456 – 2011-06-04T01:06:59.047

1edited to correct my answer while those comments were made. Ext4 DOES online defragment from what i can tell. ext 2/3 dosen't. – Journeyman Geek – 2011-06-04T01:07:03.087

3By default in Windows 7, defragmentation is scheduled for Wednesday at 1AM. If your computer is never on at this time, defragmentation may never occur. I would personally recommend that you allow the weekly scheduled defragmentation to run (or otherwise defragment weekly), because it does not take very long and it will 100% ensure that fragmentation never becomes a problem. – jcrawfordor – 2011-06-04T03:53:48.307