Monday, December 12, 2005

Why Should You Defrag Your Hard Disk?

Windows comes with a collection of house cleaning tools, including ScanDisk, Disk Defragmenter and Disk Cleanup, to help keep your disk in peak working order.

Why should you bother with the housework? A couple of reasons. First, disks are hard working, mechanical devices and, like all mechanical devices, prone to failure. A little preventative maintenance can warn you of potential problems and fix minor glitches before they can do damage to your data.

Second, the way files are organised on your drive has a perceptible impact on the performance of your computer. If your files are stored neatly, end-to-end, without fragmentation, reading and writing to the disk is speedier.

What is file fragmentation?

Sometimes when you install a program or create a data file, the file ends up chopped up into chunks and stored in multiple locations on the disk. This is called fragmentation.

What makes this happen?

When you first install your operating system and programs on your hard disk, they are written to the disk, for the most part, in one contiguous block without any gaps. The exceptions are certain system files that must be stored in specific locations. Over time, as you create and then delete documents or uninstall programs, once-filled locations are left empty and you end up with files dotted all over the disk.

Now, when Windows is writing a file to the disk, it looks for a suitable piece of free space in which to store it. What happens, then, when you copy a 40M database or audio file to the disk and the biggest slice of free space is only 30M? Or say you modify an existing file, appending a whole bunch of data so the file now takes up more space on the disk. To accommodate the files, Windows writes the first part of the file in one section of the disk and then scouts around for other places to store the rest of the file. The end result is that a single file may be stored in several chunks scattered about the disk.

Of FAT and files

Windows keeps track of each file's location in the File Allocation Table, or FAT (Windows 98 and Me use a file system called FAT32). When your file is written to disk, FAT32 provides Windows with the address of an unoccupied disk cluster. FAT32 also tells Windows on which disk sectors it will find that cluster; that is, it provides the physical location of the cluster. This information is used by your PC's BIOS (the Basic Input/Output System) to direct the actual disk writing operation.

If the file is too large to fit in a single cluster, Windows asks FAT32 for another vacant cluster, and another, and another until the whole file is written to disk. If you have lots of free clusters side by side, FAT32 can point Windows to an adjacent series of clusters, resulting in a file which occupies one contiguous chunk of the disk. If no adjacent cluster is available, FAT32 tracks down a space elsewhere on the disk and tells Windows to put the next bit of the file there; and so on until the full file is written to disk.

A record of the clusters used for storing the file is kept by FAT32 so Windows can find the file once more when you want to read it.

The fragmentation penalty

Although this all happens quickly, it makes a lot of work for your hard disk. Its read/write head, which moves across the drive platter from location to location transferring data, has to zip all over the place when saving or opening a single highly fragmented file. (By the way, many disks have more than one read/write head and multiple platters.) If a file is unfragmented, the disk head moves to one location, reads the file in one sequential swoop, and that's it.

A file stored in, say, four fragments, can easily take twice as long to open as the same file unfragmented, although the actual performance hit you take is affected by other factors, including the total size of the file.

Defragging

There's a simple solution to file fragmentation: use Windows Disk Defragmenter (Start –> Programs –> Accessories –> System Tools –> Disk Defragmenter). This utility, commonly called Defrag, gathers all the scattered file fragments and writes them into adjacent clusters, so each file occupies a contiguous section of the disk.

Defrag works by moving slabs of data to unused parts of the disk, in order to open up a large free section of space. It then assembles the fragmented parts of a file and writes them in one complete piece to the cleared space; it then does the same with the next file; and so on until the entire disk is defragmented.

Power Defrag

Disk Defragmenter is a utility which has been hobbled by the rapid advance of drive technology. It works just fine on a smallish disk of 8G or so; but use it on a 20G drive or one of the new 100G drives and you can say goodbye to using your computer for the most part of a day. (Although Microsoft says it's okay for you to use your computer while defragging, in practice this rarely works, because every write to disk causes Defrag to restart).

If you'd like to speed up Defrag and eliminate some of its problems, give Power Defrag a try.




1 comment:

Anonymous said...

great blog!!